Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.1k views
in Technique[技术] by (71.8m points)

websocket - Socketio events aren't emitting from other python files

I'm building an app using socketio and flask in python.

For the purposes of maintaining sockets and rooms I've created the following class to hold each user that connects:

class Socket:
  def __init__(self, sid):
    self.sid = sid
    self.connected = True
    self.character = None

  def emit(self, event, data):
    socketio.emit(event, data, room=self.sid)

This allows me to emit events to this user specifically. This class is declared in my server.py for the time being (my main script)

In a separate file I have the following class:

from __main__ import socketio
from __main__ import Socket

class Battle():
    def __init__(self, contestant_one=None, contestant_two=None, room_name=""):
        self.contestant_one = contestant_one
        self.contestant_two = contestant_two
        self.room_name = room_name

    def start_battle(self):
        if self.contestant_one and self.contestant_two is not None:
            self.contestant_one.emit('start battle', JSONEncoder().encode(self.contestant_one.character.__dict__))  

The above class is imported into server.py (my mains script) as seen below:

app = Flask(__name__, static_folder="static")
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)

class Socket:
def __init__(self, sid):
    self.sid = sid
    self.connected = True
    self.character = None

def emit(self, event, data):
    socketio.emit(event, data, room=self.sid)

from battle import Battle

Notice how I am importing after the socketio and socket definitions? this is according to: Flask socket.io message events in different files

contestant one and contestant two are both socket objects that are defined in server.py, and have the ability to emit messages to specific users but the event isn't being emitted in battle. I've checked in both clients browsers for the simple log I have setup to listen for that message and nothing is coming. No errors are occurring either.

JS code on client:

    socket.on('start fight', function(characters){
        console.log(characters)
    })

i've also tried testing this with print statements in the start_battle method so I know the code is executing because the prints happen.

I'll mention that I'm new to python and have worked mostly with node when it comes to socketio. Any suggestions appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 socket.on('start fight', function(characters){
    console.log(characters)
})

is listening for 'start fight' but the battle event is emitting 'start battle'

This worked after correcting it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...