Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. def sendMsg():
  2. print('send')
  3. sio.emit('client msg', '我的訊息')
  4.  
  5. from flask_socketio import SocketIO, emit
  6.  
  7. app = Flask(__name__)
  8. socketio = SocketIO( app )
  9.  
  10. clients = {}
  11.  
  12. class client:
  13. def __init__(self,sid):
  14. self.sid = sid
  15. self.connected = True
  16. def emit(self, event, data):
  17. emit(event, data, room=self.sid)
  18.  
  19. @app.route( '/' )
  20. def hello():
  21. # return render_template( './ChatApp.html' )
  22. global clients
  23. return str(len(clients))
  24.  
  25. def messageRecived():
  26. print( 'message was received!!!' )
  27.  
  28. #Add connected client
  29. @socketio.on( 'connected' )
  30. def client_connected():
  31. print ("%s connected" % (request.sid))
  32. global clients
  33. clients[request.sid]= client(request.sid)
  34. clients[request.sid].emit('response', {'sid':request.sid,'connction':clients[request.sid].connected})
  35.  
  36. @socketio.on( 'client msg' )
  37. def client_message( msg ):
  38. MSG = str(request.sid) + " 說: "+str(msg)
  39. print(MSG)
  40. socketio.emit('response',MSG)
  41.  
  42. if __name__ == '__main__':
  43. socketio.run( app, debug = True )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement