I have a Flask/SocketIO application which currently pairs two clients together to play a game together. Right now the clients are interacting with the server through some compiled client-side Javascript, and I am using socketio to define the sockets which the clients call, e.g., movedForward
when that client moved forward. The client-side JS similarly defines websockets which the server will emit to, e.g., partnerTurnedRight
when the server is passing the partner's movement to the other player.
I would like to create 'dummy' clients on the server side which can interact with a normal, remote client -- basically, a python implementation of the Javascript which is spawned every time a remote client connects. The idea is to have a server-side "player" play the game with a remote, human client.
I'm not sure how to go about implementing something like this. My intuition is that I should create a separate Flask/SocketIO app (somehow), which has sockets on for the messages the server sends (e.g., partnerTurnedRight
) and emits messages the server expects (e.g., movedForward
). Then, when a remote client connects, spawn a stateful subprocess which has its own unique sid and is able to interact with the server with the exact same interface as the remote client. However, I'm really not sure how to put everything together or how to actually spawn a server-side client like that.
An example project which does something like this, some pseudocode, or a general structure of how to set something like this up would be greatly appreciated!
(Part of the problem is that I don't know what search terms to use, so it's been hard finding examples.)
You can use the python-socketio package server-side using python. Here's an example of the client usage:
import socketio
sio = socketio.Client()
@sio.on('connect')
def on_connect():
print('connected')
sio.emit('Hello')
@sio.on('event')
def on_message(data):
print('Received ', data)
@sio.on('disconnect')
def on_disconnect():
print('disconnected')
sio.connect('http://localhost:5000')
sio.wait()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments