本文整理汇总了Python中websocket_server.WebsocketServer类的典型用法代码示例。如果您正苦于以下问题:Python WebsocketServer类的具体用法?Python WebsocketServer怎么用?Python WebsocketServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WebsocketServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, conf):
global server
super(WSService, self).__init__()
self.conf = conf
server = WebsocketServer(WSPORT, host='0.0.0.0')
server.set_fn_new_client(newClient)
server.set_fn_client_left(leftClient)
示例2: server
def server():
""" Returns the response of a server after"""
s = WebsocketServer(0, loglevel=logging.DEBUG)
server_thread = Thread(target=s.run_forever)
server_thread.daemon = True
server_thread.start()
yield s
s.server_close()
示例3: start_wss
def start_wss(port):
"start the websocket test server"
def reply_back(client, server, msg):
server.send_message(client, "You sent: %s" % msg)
server = WebsocketServer(port)
server.set_fn_message_received(reply_back)
server.run_forever()
示例4: WsServerLaunch
def WsServerLaunch():
global wsServer
while 1:
try:
wsServer = WebsocketServer( port=PORT_NUMBER + 1, host='' )
wsServer.set_fn_message_received( message_received )
wsServer.run_forever()
except Exception as e:
wsServer = None
time.sleep( 5 )
示例5: WsLapCounterServerLaunch
def WsLapCounterServerLaunch():
global wsLapCounterServer
while 1:
try:
wsLapCounterServer = WebsocketServer( port=PORT_NUMBER + 2, host='' )
wsLapCounterServer.set_fn_new_client( lap_counter_new_client )
wsLapCounterServer.run_forever()
except Exception as e:
wsLapCounterServer = None
time.sleep( 5 )
示例6: start_server
def start_server(port):
server = WebsocketServer(port)
ws_log.debug("Server listening on port: %d" % port)
server.set_fn_new_client(new_client)
server.set_fn_client_left(client_left)
server.set_fn_message_received(message_received)
server.run_forever()
示例7: listen
def listen(courier, host, port):
server = WebsocketServer(port)
server.set_fn_new_client(notify_client)
uthread = threading.Thread(target=updater, args=(courier, server))
tthread = threading.Thread(target=v_timer, args=(server,))
uthread.daemon = True
tthread.daemon = True
uthread.start()
tthread.start()
print("Starting WebSocket Server...")
server.run_forever()
示例8: __init__
def __init__(self, port=9007):
self.port = port
self.server = WebsocketServer(self.port, host='0.0.0.0')
self.server.set_fn_new_client(self.on_connect)
self.server.set_fn_message_received(self.on_msg)
self.server.set_fn_client_left(self.on_disconnect)
self.msg_lock = Lock()
示例9: __init__
def __init__(self):
super(WebSocket, self).__init__()
self.target = self.identifier
self.server = WebsocketServer(5601, "0.0.0.0")
self.wsThread = threading.Thread(target=self.server.run_forever)
self.clients = self.server.clients
self.subscriptions = []
@self.server.set_fn_new_client
def new_client(client, server):
client["subscriptions"] = []
@self.server.set_fn_client_left
def client_left(client, server):
logging.info("Client(%d) disconnected" % client['id'])
@self.server.set_fn_message_received
def message_received(client, server, msg):
if msg:
try:
event = json.loads(msg)
event_type = event["type"]
event_data = event["data"]
if event_type == "subscribe":
self._subscribe(client, event_data)
elif event_type == "unsubscribe":
self._unsubscribe(client, msg)
else:
Dispatcher().put_event(Event(type=event_type, data=event_data))
except json.decoder.JSONDecodeError as e:
pass
示例10: start_server
def start_server():
"""Start both the Game Manager and the Web Server"""
# Prepare the web server with above functions
logger.info("Init Web Socket Server")
server = WebsocketServer(PORT, HOST)
server.set_fn_new_client(onconnect)
server.set_fn_client_left(ondisconnect)
server.set_fn_message_received(onmessage)
# Create a game manager
logger.info("Init Game Manager")
global manager
manager = Manager(server)
# Start the web server
logger.info("Starting Server")
server.run_forever()
manager.safe_stop()
示例11: __init__
def __init__(self, host="0.0.0.0", port=9999):
self.frame = ""
self.nodes = ""
self.links = ""
self.last_sent_frame = ""
self.last_sent_nodes = ""
self.last_sent_links = ""
self.next_time_to_send = 0
self.lock = threading.RLock()
self.timer = None
self.ws_server = WebsocketServer(port, host)
def send_to_client(client, server):
with self.lock:
server.send_message(client, self.build_graph(True))
self.ws_server.set_fn_new_client(send_to_client)
t = threading.Thread(target=self.ws_server.run_forever)
t.daemon = True
t.start()
示例12: IPCWebSocket
class IPCWebSocket(object):
def __init__(self, ipc):
self.ipc = ipc
self.server = WebsocketServer(9000)
self._set_callbacks(self.server)
self.clients = []
def start(self):
from threading import Thread
def run():
self.server.run_forever()
t = Thread(target=run)
t.daemon = True
t.start()
def _set_callbacks(self, server):
server.set_fn_new_client(self.on_connect)
server.set_fn_client_left(self.on_disconnect)
server.set_fn_message_received(self.on_message)
def on_connect(self, client, server):
print("New client connected")
self.clients.append(client)
def on_disconnect(self, client, server):
print("Client disconnected")
self.clients = [c for c in self.clients if c['id'] != client['id']]
def on_message(self, client, server, message):
message = json.loads(message)
self.ipc.on_message(client, message)
def broadcast(self, message):
message = json.dumps(message)
for c in self.clients:
self.server.send_message(c, message)
print("broadcasted message to {} clients".format(len(self.clients)))
def send(self, client, message):
self.server.send_message(client, json.dumps(message))
示例13: GetModuleName
for mod in modlist:
mname = GetModuleName(phandle,mod)
if mname.endswith(game["moduleName"]):
memory_address = mod + game["memoryOffset"]
# read address
def readTicker():
ReadProcessMemory(phandle, memory_address, ticker, ticker_s, byref(bytesRead))
def clientJoin(client,server):
global client_num
client_num += 1
if(client_num==1):
initTicker()
def clientLeave(client,server):
global client_num
client_num -= 1
if(client_num==0):
CloseHandle(phandle)
def clientMsg(client,server,message):
readTicker()
server.send_message(client,ticker.value.replace('m','.'))
print "Starting server on " + str(socket.gethostbyname(socket.gethostname()))
server = WebsocketServer(config["serverPort"],"0.0.0.0")
server.set_fn_new_client(clientJoin)
server.set_fn_client_left(clientLeave)
server.set_fn_message_received(clientMsg)
server.run_forever()
示例14: connecting
class websocketserver:
cameras = {}
tags = {}
users = {}
calibration = {}
port=8001
# Called for every client connecting (after handshake)
def new_client_connection(self, client, server):
print("New client connected and was given id %d" % client['id'] +" and address " + str(client['address']))
server.send_message(client, "Client connected succesfully")
# Called for every client disconnecting
def client_left(self, client, server):
print("Client(%d) disconnected" % client['id'])
#TODO : Remove from list
# Called when a client sends a message
def message_received(self, client, server, message):
# print("Client(%d) said: %s" % (client['id'], message))
self.parseMessage(client, message)
def __init__(self, host='127.0.0.1'):
self.server = WebsocketServer(self.port, host)
self.server.set_fn_new_client(self.new_client_connection)
self.server.set_fn_client_left(self.client_left)
self.server.set_fn_message_received(self.message_received)
self.server.run_forever()
def parseMessage(self, client, message):
"""
Check who is the message from to redirect it to User / Tag / Camera / Calibration
or create a new instance of User / Tag / Camera / Calibration
:param client:
:param message:
:return:
"""
if self.cameras.has_key(str(client['address'])):
#print "Message from Camera"
self.cameras[str(client['address'])].push(message)
elif self.users.has_key(str(client['address'])):
print "Message from User"
elif self.tags.has_key(str(client['address'])):
print "Message from Tag"
elif self.calibration.has_key(str(client['address'])):
self.calibration[str(client['address'])].push(message)
print "Message from Calibration"
# This message is coming from an unknown client
else:
if message.split("-")[0] == "camera":
self.cameras[str(client['address'])] = Camera(client, message.split("-")[1])
# Add Observers linking every user to every camera's update
for key in self.users:
if isinstance(self.users[key], User):
self.cameras[str(client['address'])].new2DPointNotifier.addObserver(self.users[key].position.newPoint2DObserver)
self.cameras[str(client['address'])].point2DdeletedNotifier.addObserver(self.users[key].position.point2DDeletedObserver)
elif message.split("-")[0] == "tag":
print "Hello TAG"
# TODO
elif message.split("-")[0] == "user":
user = User(client, self.server, message.split("-")[1])
self.users[str(client['address'])] = user
# Add Observers linking every user to every camera's update
for key in self.cameras:
if isinstance(self.cameras[key], Camera):
self.cameras[key].new2DPointNotifier.addObserver(user.position.newPoint2DObserver)
self.cameras[key].point2DdeletedNotifier.addObserver(user.position.point2DDeletedObserver)
elif message == "calibration":
self.calibration[str(client['address'])] = Calibration(self.cameras, self.server, client)
示例15: __init__
def __init__(self, host='127.0.0.1'):
self.server = WebsocketServer(self.port, host)
self.server.set_fn_new_client(self.new_client_connection)
self.server.set_fn_client_left(self.client_left)
self.server.set_fn_message_received(self.message_received)
self.server.run_forever()