Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding websocket server
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket # pip install git+https://github.com/dpallot/simple-websocket-server.git | ||
|
||
|
||
# Load an color image in grayscale | ||
|
||
|
||
websocket_prot = 9090 | ||
|
||
clients = [] | ||
|
||
class SimpleWebSocketHub(WebSocket): | ||
|
||
def handleMessage(self): | ||
for client in clients: | ||
if client != self: | ||
client.sendMessage(self.data) | ||
|
||
|
||
def handleConnected(self): | ||
print(self.address, 'connected') | ||
clients.append(self) | ||
print("connected clients", len(clients)) | ||
|
||
|
||
|
||
def handleError(self, error): | ||
print (error) | ||
|
||
def handleClose(self): | ||
clients.remove(self) | ||
print(self.address, 'closed') | ||
print("connected clients", len(clients)) | ||
|
||
server = SimpleWebSocketServer('', websocket_prot, SimpleWebSocketHub) | ||
server.serveforever() |