├── .gitignore ├── Makefile ├── README.md ├── index.html └── wss.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | *.pyc 3 | *.pyo 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | web: 2 | python -m http.server 5001 3 | 4 | wss: 5 | python wss.py 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | arws 2 | === 3 | 4 | Arpit's own WebSocket Server. 5 | 6 | # Running it 7 | 8 | ## Running the WebSocket Server 9 | 10 | ``` 11 | make wss 12 | ``` 13 | 14 | 15 | ## Running the frontend website 16 | 17 | ``` 18 | make web 19 | ``` 20 | 21 | ### References 22 | 23 | - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#the_websocket_handshake 24 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /wss.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 | serversocket.bind(('localhost', 5000)) 5 | 6 | # queue up as many as 5 connect requests (the normal max) before refusing outside connections 7 | serversocket.listen(5) 8 | 9 | server_response_400 = """HTTP/1.1 400 Bad Request 10 | Content-Type: text/html; charset=utf-8 11 | Content-Length: 12 12 | 13 | Bad Request 14 | """ 15 | 16 | server_response_wss = """HTTP/1.1 101 Switching Protocols 17 | Upgrade: websocket 18 | Connection: Upgrade 19 | Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 20 | """ 21 | 22 | while True: 23 | (clientsocket, address) = serversocket.accept() 24 | print("client connected on", address) 25 | 26 | # Client handshake request 27 | # The client will send a pretty standard HTTP request with headers that looks like this 28 | # (the HTTP version must be 1.1 or greater, and the method must be GET) 29 | # 30 | # 31 | # GET /socket.io/?EIO=4&transport=polling&t=Nl5IOYG HTTP/1.1 32 | # Host: localhost:5000 33 | # User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 34 | # Accept: */* 35 | # Accept-Language: en-US,en;q=0.5 36 | # Accept-Encoding: gzip, deflate 37 | # Origin: http://localhost:5001 38 | # Connection: keep-alive 39 | # Referer: http://localhost:5001/ 40 | # Sec-Fetch-Dest: empty 41 | # Sec-Fetch-Mode: cors 42 | # Sec-Fetch-Site: cross-site 43 | request = clientsocket.recv(1024).decode("utf-8") 44 | print(request) 45 | 46 | # When the server receives the handshake request, it should send back a 47 | # special response that indicates that the protocol will be changing 48 | # from HTTP to WebSocket. 49 | clientsocket.send(server_response_wss.encode('ascii')) 50 | 51 | 52 | clientsocket.close() 53 | --------------------------------------------------------------------------------