morrissinger / ESP8266-Websocket

A websocket library for the ESP-8266.
305 stars 119 forks source link

How to create own websocket server? #18

Open williamesp2015 opened 8 years ago

williamesp2015 commented 8 years ago

Hi, this is very interesting websocket client. It is working when connecting to echo.websocket.org but if change host url to my url and copy the same echo.websocket into index.html it does not work. Any help would be appreciated.

KochC commented 7 years ago

Where did you find the code? The current example is with another board... :/

dphans commented 6 years ago

Below is simple websocket server wrote with Python 3.6 (Using flask Flask-Sockets), you can preference:

from flask import Flask
from flask_sockets import Sockets

app = Flask(__name__)
sockets = Sockets(app)

@sockets.route('/')
def echo_socket(ws):
    while not ws.closed:
        message = ws.receive()
        ws.send(message)

@app.route('/')
def hello():
    return 'Server working!'

if __name__ == "__main__":
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    server = pywsgi.WSGIServer(('', 5000), application=app, handler_class=WebSocketHandler)
    server.serve_forever()

Now you can start by save file as app.py then run from terminal: python3.6 app.py. First, connect to: ws://127.0.0.1:500/ then try to _websocketInstance.sendData("Hello, world!"), you will received Hello, world! from _websocketInstance.getData(_receivedDataStringInstance)!