BetaRavener/upy-websocket-server

About port to Pyboard

Closed this issue · 2 comments

Thank you very much for your work of upy-websocket-server.
I successfully ran upy-websocket-server on Pyboard, but there are a few problems here.
I use Wiznet to make Pyboard online.

import pyb
import network
nic=network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
nic.ifconfig(('192.168.0.18', '255.255.255.0','192.168.0.1', '8.8.8.8'))
nic.active(1)

In start() ,when I use self._setup_conn(port, self._accept_conn) , I can't make my client access the server.
I made some changes in start().

def start(self, port=80):
    if self._listen_s:
        self.stop()
    # self._setup_conn(port, self._accept_conn)
    self._setup_conn(port, None)
    while self.IsWebsocket==0:
        self._accept_conn(self._listen_s)
    print("Started WebSocket server.")

When Websocket is connected , self.IsWebsocket becomes 1.
In this way, I can get the html page and CSS file normally, and connected websocket.
Even though I set max_connections to 10, I can only connect to one websocket.
How can I achieve multiple connections?

Well I guess it's because once you get websocket connection, as you said "self.IsWebsocket becomes 1", thus this loop condition will be always false while self.IsWebsocket==0: and you are no longer able to receive connections. In fact, your changes doesn't make much sense. First, you actively wait for connection (with while loop) which prevents the board from doing anything else. Also for some reason, you set None as handler function for new connections. And even if everything else was correct, you'd have to call start in a loop because that's where you accept new connections. But this wouldn't be correct as it would always restart the server.

If your board has problems with setsockopt which is used to install handler (which might be the case as accepting connection manually solves your problem), try files in Poll folder in this repository, it might work better for you.

Thank you for your reply.
I solved this problem by import thread and uasyncio. In this way, I can access my server concurrently.

Because I put the code that connects to the network outside of _setup_conn, it doesn't appear to restart the server every time.