def run(): '''Run the interactive app.''' # Get the channel ID channel_id = userdata()['channel']['id'] # Get Interactive connection information. data = join_interactive(channel_id) # Initialize a connection with Interactive. connection = yield from start(data['address'], channel_id, data['key']) # Handlers, to be called when Interactive packets are received. handlers = { proto.id.error: on_error, proto.id.report: on_report } # wait_message is a coroutine that will return True when it receives # a complete packet from Interactive, or False if we got disconnected. while (yield from connection.wait_message()): # Decode the Interactive packet. decoded, _ = connection.get_packet() packet_id = proto.id.get_packet_id(decoded) # Handle the packet with the proper handler, if its type is known. if packet_id in handlers: handlers[packet_id](decoded) elif decoded is None: print('Unknown bytes were received. Uh oh!', packet_id) else: print('We got packet {} but didn\'t handle it!'.format(packet_id)) connection.close()
def connect(): session = Session() channel_id = login(session, **auth)['channel']['id'] print("channel_id") data = get_tetris(session, channel_id) conn = yield from start(data['address'], channel_id, data['key'], loop) handlers = { proto.id.error: on_error, proto.id.report: on_report } while (yield from conn.wait_message()): decoded, packet_bytes = conn.get_packet() packet_id = proto.id.get_packet_id(decoded) if decoded is None: print('We got a bunch of unknown bytes.') print(packet_id) elif packet_id in handlers: handlers[packet_id](decoded, conn) else: print("We got packet {} but didn't handle it!".format(packet_id)) conn.close()
def connect(): # Initialize session, authenticate to Beam servers, and retrieve Tetris # address and key. session = Session() channel_data = login(session, **auth) if not "channel" in channel_data: raise AuthenticationException("Incorrect username or password") channel_id = channel_data["channel"]["id"] data = get_tetris(session, channel_id) interactiveData = go_interactive(session, channel_id, game_version, share_code) #Our tactile key codes can be retrieved from the get_controls routine controls = get_controls(session, channel_id) #The tactiles live in version.controls.tactiles tactiles = controls["version"]["controls"]["tactiles"] #tactiles can now easily be used to map a tactile id from an incoming report #to a tactile keycode from the controls editor for tactile in tactiles: print("ID {} is key {}".format(tactile["id"],tactile["key"])) # start() takes the remote address of Beam Interactive, the channel # ID, and channel the auth key. This information can be obtained # via the backend API, which is documented at: # https://developer.beam.pro/api/v1/ conn = yield from start(data['address'], channel_id, data['key'], loop) handlers = { proto.id.error: on_error, proto.id.report: on_report, proto.id.handshake: on_handshake, proto.id.handshake_ack: on_handshake_ack } # wait_message is a coroutine that will return True when we get # a complete message from Beam Interactive, or False if we # got disconnected. while (yield from conn.wait_message()): decoded, packet_bytes = conn.get_packet() packet_id = proto.id.get_packet_id(decoded) if decoded is None: print('We got a bunch of unknown bytes.') elif packet_id in handlers: handlers[packet_id](decoded, conn) else: print("We got packet {} but didn't handle it!".format(packet_id)) print("Closing") conn.close()
def run(): """Run the interactive app.""" # Authenticate with Beam and retrieve the channel id from the response. channel_id = login( # **AUTHENTICATION is a cleaner way of doing this. AUTHENTICATION["username"], AUTHENTICATION["password"], AUTHENTICATION["code"] )["channel"]["id"] # Get Interactive connection information. data = join_interactive(channel_id) # Initialize a connection with Interactive. connection = yield from start(data["address"], channel_id, data["key"]) # Handlers, to be called when Interactive packets are received. handlers = { proto.id.error: on_error, proto.id.report: on_report } # wait_message is a coroutine that will return True when it receives # a complete packet from Interactive, or False if we got disconnected. while (yield from connection.wait_message()): # Decode the Interactive packet. decoded, _ = connection.get_packet() packet_id = proto.id.get_packet_id(decoded) # Handle the packet with the proper handler, if its type is known. if packet_id in handlers: handlers[packet_id](decoded) elif decoded is None: print("Unknown bytes were received. Uh oh!", packet_id) else: print("We got packet {} but didn't handle it!".format(packet_id)) connection.close()
def test_handshakes(self): conn = yield from start(self.echo, 42, "asdf", loop=self.loop) self.assertTrue(conn.open) self.assertFalse(conn.closed) self.assertTrue((yield from conn.wait_message())) decoded, bytes = conn.get_packet() self.assertEqual(42, decoded.channel) self.assertEqual("asdf", decoded.streamKey) err = Error() err.message = "foo" yield from conn.send(err) self.assertTrue((yield from conn.wait_message())) decoded, bytes = conn.get_packet() self.assertEqual("foo", err.message) conn.close() self.assertFalse((yield from conn.wait_message())) self.assertFalse(conn.open) self.assertTrue(conn.closed)
def test_handshakes(self): conn = yield from start(self.echo, 42, 'asdf', loop=self.loop) self.assertTrue(conn.open) self.assertFalse(conn.closed) self.assertTrue((yield from conn.wait_message())) decoded, bytes = conn.get_packet() self.assertEqual(42, decoded.channel) self.assertEqual('asdf', decoded.streamKey) err = Error() err.message = 'foo' yield from conn.send(err) self.assertTrue((yield from conn.wait_message())) decoded, bytes = conn.get_packet() self.assertEqual('foo', err.message) conn.close() self.assertFalse((yield from conn.wait_message())) self.assertFalse(conn.open) self.assertTrue(conn.closed)