Clients: ?
demo

http://pyyyc.meejah.ca

mike warren
https://meejah.ca

table of contents

overview

why WAMP?

WAMP

Autobahn

Crossbar

demo?

boilerplate

from autobahn.twisted import wamp
# from autobahn.asyncio import wamp
class Game(wamp.ApplicationSession):
    def onJoin(self, details):
        """intersting code goes here"""
if __name__ == '__main__':
    runner = wamp.ApplicationRunner(
        u'ws://pyyyc.meejah.ca/ws',
        u'demo',
    )
    runner.run(Game)

backend

class Client(object):
    def __init__(self, name, color):
        self.name = name
        self.color = color
        # a 16x16 grid of False values
        self._state = []
        for y in range(16):
            self._state.append([False] * 16)

    def click(self, x, y):
        self._state[x][y] = not self._state[x][y]
class Board(object):
    def __init__(self):
        # client_id -> Client instance
        self._clients = {}
        self._color_generator = itertools.cycle(COLORS)
    def click(self, client_id, x, y):
        self._clients[client_id].click(x, y)
    def client_add(self, client_id):
        color = next(self._color_generator)
        self._clients[client_id] = Client(client_id, color)
    def client_del(self, client_id):
        del self._clients[client_id]
class Board(object):
    # ...
    @wamp.register(u"meejah.get_client_count")
    def client_count(self):
        return len(self._clients)
    @wamp.register(u"meejah.get_board")
    def as_json(self):
        state = []
        for x in range(16):
            column = []
            for y in range(16):
                column.append(self._get_pixel(x, y))
            state.append(column)
        return state
class Board(object):
    # ...
    def _get_pixel(self, x, y):
        value = (0, 0, 0)
        for client in self._clients.values():
            if client._state[x][y]:
                value = (
                    max(value[0], client.color[0]),
                    max(value[1], client.color[1]),
                    max(value[2], client.color[2]),
                )
        return value
class Game(ApplicationSession):
    @defer.inlineCallbacks
    def onJoin(self, details):
        self._board = Board()
        yield self.register(self._board)
        yield self.subscribe(self)
        yield self.register(
            self._click, u"meejah.click",
            options=RegisterOptions(details_arg="details"),
        )
class Game(ApplicationSession):
    # ...
    @wamp.subscribe(u"wamp.session.on_leave")
    def _session_left(self, session):
        self._board.client_del(session)
        return self.publish(u"meejah.board_invalid")
    @wamp.subscribe(u"wamp.session.on_join")
    def _session_join(self, details):
        self._board.client_add(details['session'])
        return self.publish(u"meejah.board_invalid")
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
import random
from twisted.internet.defer import inlineCallbacks as coroutine
from autobahn.twisted.util import sleep
class RandomAgent(ApplicationSession):
    @coroutine
    def onJoin(self, details):
        while True:
            yield self.call(
                u"meejah.click",
                random.randint(0, 15),
                random.randint(0, 15),
            )
            yield sleep(.5)
if __name__ == '__main__':
    runner = ApplicationRunner(u'ws://pyyyc.meejah.ca/ws', u'demo')
    runner.run(RandomAgent)
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
import random
from asyncio import coroutine, sleep
class RandomAgent(ApplicationSession):
    @coroutine
    def onJoin(self, details):
        while True:
            yield from self.call(
                u"meejah.click",
                random.randint(0, 15),
                random.randint(0, 15),
            )
            yield from sleep(.5)
if __name__ == '__main__':
    runner = ApplicationRunner(u'ws://pyyyc.meejah.ca/ws', u'demo')
    runner.run(RandomAgent)

http://crossbar.io

http://autobahn.ws