python-websocket-server
python-websocket-server copied to clipboard
Support channels?
Hello,
I testing this proyect, I was wondering if:
can I define canals to transmite a message?
Is this possble?
e.g.: "ws://localhost:port/Channel1" "ws://localhost:port/Channel2"
So, with this configuration I could connect as a Client with this topic, for example to the "Channel1", and transmit news, and then I could to connect to the topic "Channel2", and transmit errors.
Thanks for all!
Hello, I was facing the same thing, so I did the following changes to the /websocket-server/websocket_server.py file : Change in WebSocketHandler class:
def read_http_headers(self):
headers = {}
# first line should be HTTP GET
http_get = self.rfile.readline().decode().strip()
# Next three lines will get your end_point
global with_end_point
if re.search('\/([A-Za-z0-9]+)', http_get).group(1):
with_end_point = re.search('\/([A-Za-z0-9]+)', http_get).group(1)
assert http_get.upper().startswith('GET')
# remaining should be headers
while True:
header = self.rfile.readline().decode().strip()
if not header:
break
head, value = header.split(':', 1)
headers[head.lower().strip()] = value.strip()
return headers
Change in WebsocketServer class:
def _new_client_(self, handler):
self.id_counter += 1
# This if-else block will check if you have provided an end_point and if you have, it assigns that
# endpoint as the 'Id' of the socket connection, otherwise it will assign a number as 'Id'
if with_end_point:
client = {
'id': with_end_point,
'handler': handler,
'address': handler.client_address
}
else:
client = {
'id': self.id_counter,
'handler': handler,
'address': handler.client_address
}
self.clients.append(client)
self.new_client(client, self)
And finally in your main script, use sever.send_message method to send the message to specific id or end point. Hope this helps.