-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (net,websocket,vweb) updates the websocket server to use an oth…
…er server to handle handshake resquests - adds `is_headless` attribute to `webscokect.Server` - adds public function to handle handcheck from a `TcpConn` for a given key - adds a `vweb` example - adds `Sec-WebSocket-Key` as common HTTP header
- Loading branch information
christophe
committed
Dec 6, 2023
1 parent
55e86fe
commit 8174157
Showing
5 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const messageList = document.getElementById('message-list'); | ||
const protocol = location.protocol === 'https:' ? 'wss' : 'ws'; | ||
const socket = new WebSocket(`${protocol}://${location.host}/ws`); | ||
let i = 0; | ||
|
||
function send(message) { | ||
messageList.innerHTML += `<li>> ${message}</li>`; | ||
socket.send(message); | ||
} | ||
|
||
socket.addEventListener("open", (event) => { | ||
console.log('Connected to WS server'); | ||
send('Salut tout le monde !'); | ||
}); | ||
|
||
socket.addEventListener("message", (event) => { | ||
const { data } = event; | ||
messageList.innerHTML += `<li>< ${data}</li>`; | ||
setTimeout(() => { | ||
send(`Message reçu ${i++}`); | ||
}, 3000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html lang=en> | ||
<head> | ||
<meta charset=utf-8> | ||
<title>vweb websocket example page</title> | ||
</head> | ||
<body> | ||
<ol id="message-list"></ol> | ||
<script type="text/javascript" src="websocket_client.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module main | ||
|
||
import log | ||
import net.http | ||
import net.websocket | ||
import term | ||
import vweb | ||
|
||
const http_port = 8080 | ||
|
||
struct App { | ||
vweb.Context | ||
mut: | ||
wss &websocket.Server @[vweb_global] | ||
} | ||
|
||
fn slog(message string) { | ||
eprintln(term.colorize(term.bright_yellow, message)) | ||
} | ||
|
||
fn clog(message string) { | ||
eprintln(term.colorize(term.cyan, message)) | ||
} | ||
|
||
fn wlog(message string) { | ||
eprintln(term.colorize(term.bright_blue, message)) | ||
} | ||
|
||
fn main() { | ||
mut app := new_app() or { panic(err) } | ||
vweb.run(app, http_port) | ||
} | ||
|
||
fn new_app() !&App { | ||
mut app := &App{ | ||
wss: new_websocker_server()! | ||
} | ||
app.handle_static('assets', true) | ||
return app | ||
} | ||
|
||
fn new_websocker_server() !&websocket.Server { | ||
mut wss := &websocket.Server{ | ||
logger: &log.Log{ | ||
level: .debug | ||
} | ||
is_headless: true | ||
} | ||
wss.on_connect(fn (mut server_client websocket.ServerClient) !bool { | ||
slog('ws.on_connect, server_client.client_key: ${server_client.client_key}') | ||
return true | ||
})! | ||
wss.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ! { | ||
slog('s.on_message msg.opcode: ${msg.opcode} | msg.payload: ${msg.payload}') | ||
ws.write(msg.payload, msg.opcode) or { | ||
eprintln('ws.write err: ${err}') | ||
return err | ||
} | ||
}) | ||
wss.on_close(fn (mut ws websocket.Client, code int, reason string) ! { | ||
slog('s.on_close code: ${code}, reason: ${reason}') | ||
}) | ||
slog('Websocket Server initialized') | ||
return wss | ||
} | ||
|
||
pub fn (mut app App) index() vweb.Result { | ||
return $vweb.html() | ||
} | ||
|
||
pub fn (mut app App) ws() !vweb.Result { | ||
key := app.req.header.get(http.CommonHeader.sec_websocket_key)! | ||
app.wss.handle_handcheck(mut app.conn, key) or { | ||
wlog('handle_handcheck error: ${err.msg()}') | ||
return err | ||
} | ||
return app.text('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters