Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support channels? #50

Open
matiasz8 opened this issue May 17, 2018 · 1 comment
Open

Support channels? #50

matiasz8 opened this issue May 17, 2018 · 1 comment

Comments

@matiasz8
Copy link

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!

@PriyankBangar
Copy link

PriyankBangar commented Jun 14, 2018

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.

@Pithikos Pithikos changed the title Can I Specify topics? Support channels? Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants