main
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"
WebSocket connection to 'ws://192.168.0.174:8080/' failed: Invalid frame header open-ws-connection @ (index):74
ws = new WebSocket('ws://192.168.0.174:8080');
if request.path == "/"
main:
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"
ws = new WebSocket('ws://$(addr_str):8080/ws');
/ws
--max-tasks=2
server := http.Server --max-tasks=2
else
if request.path == "/"
jag run counter.toit
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}
Starting 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}
status 200, method get, file /
status 404, method get, file favicon.ico
status 400, method get, file ws
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]