1#!/usr/bin/python 2 3import sys 4from os.path import dirname 5sys.path.append(dirname(__file__) + "/python-websocket-server") 6 7from websocket_server import WebsocketServer 8import logging 9 10# Called for every client connecting (after handshake) 11def new_client(client, server): 12 print("New client connected and was given id %d" % client['id']) 13 #server.send_message_to_all("Hey all, a new client has joined us") 14 15# Called for every client disconnecting 16def client_left(client, server): 17 print("Client(%d) disconnected" % client['id']) 18 19# Called when a client sends a message 20def message_received(client, server, message): 21 print("Client(%d) sent[%d]" % (client['id'], len(message))) 22 server.send_message(client, message) 23 24PORT=9001 25server = WebsocketServer(PORT, host="192.0.2.2", loglevel=logging.INFO) 26server.set_fn_new_client(new_client) 27server.set_fn_client_left(client_left) 28server.set_fn_message_received(message_received) 29server.run_forever() 30