davidg238
12/29/2023, 7:56 PMmain
initially to look like:
import http
import http.request show RequestIncoming
import http.web_socket
import net
/**
Example that demonstrates a web-socket server.
The server updates a counter for button presses.
*/
network := net.open
addr_str := network.address.stringify
server := http.Server
main:
print "Open a browser on: $network.address:8080"
sessions := {:}
server.listen network 8080:: | request/RequestIncoming response/http.ResponseWriter |
print "enter handler"
websocket := server.web_socket request response
if websocket:
print "ws is: $websocket"
else:
print "request $request.method $request.path $request.body.read"
if request.path == "/":
response.write spa
else:
print "request $request.method $request.path $request.body.read failed"
print "exit handler"
When the page loads, I get:
WebSocket connection to 'ws://192.168.0.174:8080/' failed: Invalid frame header open-ws-connection @ (index):74
which corresponds to the line:
ws = new WebSocket('ws://192.168.0.174:8080');
in the page javascript.
Is this a bug in the new websocket code or cockpit error (as Session.upgrade is replaced)?erikcorry
01/02/2024, 9:06 AMerikcorry
01/02/2024, 9:07 AMerikcorry
01/02/2024, 9:07 AMerikcorry
01/02/2024, 9:08 AMerikcorry
01/02/2024, 9:09 AMif request.path == "/"
and sends it HTML on the websocket connection, which messes things up.erikcorry
01/02/2024, 9:17 AMmain:
print "Open a browser on: $network.address:8080"
sessions := {:}
server.listen network 8080:: | request/RequestIncoming response/http.ResponseWriter |
if request.path == "/ws":
websocket := server.web_socket request response
if websocket:
print "ws is: $websocket"
else if request.path == "/":
response.write SPA
else:
print "request $request.method $request.path $request.body.read failed"
response.headers.set "Content-Type" "text/plain"
response.write_headers 404
response.write "Not found\n"
erikcorry
01/02/2024, 9:18 AMws = new WebSocket('ws://$(addr_str):8080/ws');
with the /ws
at the end.erikcorry
01/02/2024, 9:19 AM--max-tasks=2
at least when creating the HTTP server.erikcorry
01/02/2024, 9:19 AMserver := http.Server --max-tasks=2
erikcorry
01/02/2024, 9:20 AMerikcorry
01/02/2024, 9:22 AMelse
in front of the if request.path == "/"
which makes sure you don't do the whole router when it's a websocket connection.davidg238
01/02/2024, 4:17 PMdavidg238
01/04/2024, 11:20 PMjag run counter.toit
and view the page on Chrome, I get:
Starting web server at 192.168.0.137:8080
DEBUG: client connected {peer: 192.168.0.174:38064}
DEBUG: incoming request {peer: 192.168.0.174:38064, path: /}
DEBUG: client connected {peer: 192.168.0.174:38078}
<after ~30 seconds>
DEBUG: connection ended {peer: 192.168.0.174:38078, reason: DEADLINE_EXCEEDED}
DEBUG: client connected {peer: 192.168.0.174:38082}
DEBUG: incoming request {peer: 192.168.0.174:38082, path: /ws}
websocket request invoked
DEBUG: connection ended {peer: 192.168.0.174:38064, reason: DEADLINE_EXCEEDED}
DEBUG: client socket detached {peer: 192.168.0.174:38082}
I get the first 3 lines on the console, and the page renders immediately.
The websocket shows "pending" in the Chrome developer tools/network tab.
Afer about 30 seconds, the websocket status changes to 101 and the button works.
There is no apparent request for the favicon.icodavidg238
01/04/2024, 11:21 PMStarting web server at 192.168.0.137:8080
DEBUG: client connected {peer: 192.168.0.174:35648}
DEBUG: incoming request {peer: 192.168.0.174:35648, path: /}
DEBUG: client connected {peer: 192.168.0.174:35664}
DEBUG: incoming request {peer: 192.168.0.174:35664, path: /ws}
websocket request invoked
DEBUG: connection ended {peer: 192.168.0.174:35664}
DEBUG: connection ended {peer: 192.168.0.174:35648, reason: DEADLINE_EXCEEDED}
immediately. In the developer/network tab,
In the URL window:
status 200, method get, file /
status 404, method get, file favicon.ico
status 400, method get, file ws
In the request window:
GET http://192.168.0.137:8080/ [HTTP/1.1 200 ok 190ms]
GET http://192.168.0.137:8080/favicon.ico [HTTP1.1 404 Not Found 0ms]
GET ws://192.168.0.137:8080/ws [HTTP/1.1 400 No Connection: Upgrade 41mS]
hhv0001
01/05/2024, 8:20 AMdavidg238
01/05/2024, 2:54 PMdavidg238
01/05/2024, 2:55 PM