net/websocket
A two way connection that stays open between browser and server, with rooms for broadcasting.
import net.websocket
A WebSocket is a connection that stays open after one HTTP request, and either side sends a message whenever it likes; for live chat, notifications and games. The server takes connections with Listen and Accept, and a client connects with Dial.
To serve several clients, read each connection in its own thread and use a Hub to send a message to every member of a room.
Types
Server
struct Server| Name | Type | Description |
|---|---|---|
fd | i64 = -1 | The socket descriptor. |
port | int = 0 | The port. |
import net.websocket
mut server := websocket.Server {}
func responder():
mut conn := websocket.Accept(server)
until websocket.IsOpen(conn):
m := websocket.Receive(conn)
if websocket.IsText(m):
_ := websocket.SendText(conn, "echo: " + m.text)
else websocket.IsBinary(m):
_ := websocket.SendBinaryBytes(conn, m.data, m.len)
else websocket.IsClose(m):
break
end
end
websocket.Close(conn)
end
func main:
server = websocket.Listen(0)
println websocket.Ok(server), websocket.Port(server) > 0
t := spawn(responder)
mut c := websocket.Dial("127.0.0.1", websocket.Port(server), "/chat")
println websocket.IsOpen(c), c.path, c.client
_ := websocket.SendText(c, "hello")
println websocket.ReceiveText(c)
_ := websocket.SendBinary(c, "abc")
m := websocket.Poll(c, 2000)
println m.kind == websocket.BINARY, m.len
_ := websocket.Ping(c, "p")
m2 := websocket.Poll(c, 2000)
println websocket.IsPong(m2), m2.kind == websocket.PONG
m3 := websocket.Poll(c, 50)
println m3.kind == websocket.TIMEOUT
websocket.Close(c)
join(t)
websocket.CloseServer(server)
println websocket.NONE, websocket.TEXT, websocket.CLOSE, websocket.PING
endConn
struct Conn| Name | Type | Description |
|---|---|---|
fd | i64 = -1 | The socket descriptor. |
open | bool = false | The connection is open. |
path | str = "" | The path that was asked for, such as /chat. |
client | bool = false | This is the client side; a client sends its frames masked. |
buf | void* | The receive buffer. |
blen | int = 0 | The length of the data in the buffer. |
bcap | int = 0 | The buffer capacity. |
secure | bool = false | It is encrypted. |
tls_idx | int = -1 | The internal TLS index. |
hdr | str = "" | The handshake headers. |
acc | str = "" | The text of a multi frame message being gathered. |
acc_kind | int = 0 | The kind of the multi frame message. |
fragmented | bool = false | A multi frame message is on its way. |
bin | void* | The buffer of a binary message. |
binlen | int = 0 | Its length. |
bincap | int = 0 | Its capacity. |
The example for this item is shown with the ones above: net-websocket-Echo.salam
Message
struct Message| Name | Type | Description |
|---|---|---|
kind | int = 0 | The message kind; one of the constants above. |
text | str = "" | The text of a text message. |
data | void* | The data of a binary message. |
len | int = 0 | The length of the data. |
The example for this item is shown with the ones above: net-websocket-Echo.salam
RoomMembers
struct RoomMembers| Name | Type | Description |
|---|---|---|
items | Vector | The connections. |
import net.websocket
mut server := websocket.Server {}
mut sides := Vector {} as Vector<websocket.Conn>
func acceptor():
repeat 2:
sides.push(websocket.Accept(server))
end
end
func main:
server = websocket.Listen(0)
port := websocket.Port(server)
t := spawn(acceptor)
mut a := websocket.Dial("127.0.0.1", port, "/room")
mut b := websocket.DialWith("127.0.0.1", port, "/room", "X-Name: b\r\n")
join(t)
mut hub := websocket.NewHub()
each side in sides:
websocket.Register(hub, "lobby", side)
end
println websocket.RoomSize(hub, "lobby"), hub.rooms.size()
println websocket.Broadcast(hub, "lobby", "welcome")
println websocket.ReceiveText(a), websocket.ReceiveText(b)
println websocket.BroadcastBinary(hub, "lobby", "xy")
println websocket.Receive(a).len
websocket.Unregister(hub, "lobby", sides[0])
println websocket.RoomSize(hub, "lobby")
websocket.Stop(a)
websocket.Close(b)
websocket.CloseServer(server)
endHub
struct Hub| Name | Type | Description |
|---|---|---|
rooms | HashMap | The rooms and their members. |
The example for this item is shown with the ones above: net-websocket-Hub.salam
Constants and variables
NONE
const NONE: i32 = 0The example for this item is shown with the ones above: net-websocket-Echo.salam
TEXT
const TEXT: i32 = 1The example for this item is shown with the ones above: net-websocket-Echo.salam
BINARY
const BINARY: i32 = 2The example for this item is shown with the ones above: net-websocket-Echo.salam
CLOSE
const CLOSE: i32 = 8The example for this item is shown with the ones above: net-websocket-Echo.salam
PING
const PING: i32 = 9The example for this item is shown with the ones above: net-websocket-Echo.salam
PONG
const PONG: i32 = 10The example for this item is shown with the ones above: net-websocket-Echo.salam
TIMEOUT
const TIMEOUT: i32 = 100The example for this item is shown with the ones above: net-websocket-Echo.salam
Functions
AcceptKey
func AcceptKey(key: str): strimport net.websocket
import str
func main:
println websocket.AcceptKey("dGhlIHNhbXBsZSBub25jZQ==")
request := websocket.HandshakeRequest("example.com", "/chat", "dGhlIHNhbXBsZSBub25jZQ==")
println str.Split(request, "\r\n")[0]
request2 := websocket.HandshakeRequestWith("example.com", "/chat", "dGhlIHNhbXBsZSBub25jZQ==", "Authorization: Bearer x\r\n")
println str.Contains(request2, "Authorization: Bearer x")
response := websocket.HandshakeResponse(request)
println str.Split(response, "\r\n")[0]
println str.Contains(response, "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
endHandshakeResponse
func HandshakeResponse(request: str): strThe example for this item is shown with the ones above: net-websocket-Handshake.salam
HandshakeRequest
func HandshakeRequest(host: str, path: str, key: str): strThe example for this item is shown with the ones above: net-websocket-Handshake.salam
HandshakeRequestWith
func HandshakeRequestWith(host: str, path: str, key: str, extra: str): strThe example for this item is shown with the ones above: net-websocket-Handshake.salam
Listen
func Listen(port: int): ServerThe example for this item is shown with the ones above: net-websocket-Echo.salam
Ok
func Ok(srv: Server): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
Port
func Port(srv: Server): intThe example for this item is shown with the ones above: net-websocket-Echo.salam
CloseServer
func CloseServer(srv: Server)The example for this item is shown with the ones above: net-websocket-Echo.salam
Accept
func Accept(srv: Server): ConnThe example for this item is shown with the ones above: net-websocket-Echo.salam
LoadWSSConfig
func LoadWSSConfig(cert_file: str, key_file: str)import net.websocket
func main:
mut a := websocket.DialWSS("echo.example.com", 443, "/")
mut b := websocket.DialWSSWith("echo.example.com", 443, "/", "Origin: https://salamlang.ir\r\n")
mut c := websocket.DialWSSUnverified("localhost", 8443, "/", "")
println websocket.IsOpen(a), websocket.IsOpen(b), websocket.IsOpen(c)
websocket.LoadWSSConfig("cert.pem", "key.pem")
s := websocket.Listen(8443)
mut conn := websocket.AcceptWSS(s)
_ := websocket.SendText(conn, "hello")
websocket.Close(conn)
endAcceptWSS
func AcceptWSS(srv: Server): ConnThe example for this item is shown with the ones above: net-websocket-WSS.salam
Dial
func Dial(host: str, port: int, path: str): ConnThe example for this item is shown with the ones above: net-websocket-Echo.salam
DialWith
func DialWith(host: str, port: int, path: str, extra: str): Conn\r\n.The example for this item is shown with the ones above: net-websocket-Hub.salam
DialWSS
func DialWSS(host: str, port: int, path: str): ConnThe example for this item is shown with the ones above: net-websocket-WSS.salam
DialWSSWith
func DialWSSWith(host: str, port: int, path: str, extra: str): ConnThe example for this item is shown with the ones above: net-websocket-WSS.salam
DialWSSUnverified
func DialWSSUnverified(host: str, port: int, path: str, extra: str): ConnThe example for this item is shown with the ones above: net-websocket-WSS.salam
IsOpen
func IsOpen(c &: Conn): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
Receive
func Receive(c &: Conn): MessageThe example for this item is shown with the ones above: net-websocket-Echo.salam
Poll
func Poll(c &: Conn, timeout_ms: int): MessageThe example for this item is shown with the ones above: net-websocket-Echo.salam
ReceiveText
func ReceiveText(c &: Conn): strThe example for this item is shown with the ones above: net-websocket-Echo.salam
IsText
func IsText(m: Message): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
IsBinary
func IsBinary(m: Message): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
IsClose
func IsClose(m: Message): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
IsPong
func IsPong(m: Message): boolThe example for this item is shown with the ones above: net-websocket-Echo.salam
SendText
func SendText(c &: Conn, msg: str): intThe example for this item is shown with the ones above: net-websocket-Echo.salam
SendBinary
func SendBinary(c &: Conn, data: str): intThe example for this item is shown with the ones above: net-websocket-Echo.salam
SendBinaryBytes
func SendBinaryBytes(c &: Conn, ptr: void*, n: int): intThe example for this item is shown with the ones above: net-websocket-Echo.salam
Ping
func Ping(c &: Conn, data: str): intThe example for this item is shown with the ones above: net-websocket-Echo.salam
Close
func Close(c &: Conn)The example for this item is shown with the ones above: net-websocket-Echo.salam
Stop
func Stop(c &: Conn)The example for this item is shown with the ones above: net-websocket-Hub.salam
NewHub
func NewHub(): HubThe example for this item is shown with the ones above: net-websocket-Hub.salam
Register
func Register(h &: Hub, room: str, c: Conn)The example for this item is shown with the ones above: net-websocket-Hub.salam
Unregister
func Unregister(h &: Hub, room: str, c: Conn)The example for this item is shown with the ones above: net-websocket-Hub.salam
RoomSize
func RoomSize(h: Hub, room: str): intThe example for this item is shown with the ones above: net-websocket-Hub.salam
Broadcast
func Broadcast(h &: Hub, room: str, msg: str): intThe example for this item is shown with the ones above: net-websocket-Hub.salam
BroadcastBinary
func BroadcastBinary(h &: Hub, room: str, data: str): intThe example for this item is shown with the ones above: net-websocket-Hub.salam