swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.04k stars 389 forks source link

How to connect on websocket? #516

Closed evecimar closed 2 years ago

evecimar commented 2 years ago

Make sure you read Issues Guideline and answer these questions before submitting your issue. Thanks! (Any non-English issues will be closed immediately.)

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole) PHP 7.4.13 Swoole 4.8.6

  2. Please provide your Laravel/Lumen version. Laravel Framework 6.20.44

  3. Which release version of this package are you using? swooletw/laravel-swoole 2.11

  4. What did you do? If possible, provide a recipe for reproducing the error. I enabled the websocket server, but when I connection in websocket using Python or Chtome websocket, the conecte is closing

  5. What did you expect to see? I hope the connection remains

  6. What did you see instead?

on_message
0{"sid":"NjIwMWNhZGM0NTc3NA==","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
on_message
40
on_message
42["message","connected"]
### closed ###

Process finished with exit code 0

My simple python code:

import websocket
import _thread
import time

def on_message(ws, message):
    print('on_message')
    print(message)

def on_error(ws, error):
    print('error')
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send('welcome3')

        time.sleep(1)
        ws.close()
        print("thread terminating...")
    _thread.start_new_thread(run, ())

if __name__ == "__main__":

    ws = websocket.WebSocketApp("ws://localhost:1215/",
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close,
                            )

    ws.run_forever()