Standard library · Network

net/socketio

A Socket.IO client: events, acknowledgements, namespaces and automatic reconnection.

import net.socketio

Socket.IO is the usual live messaging library in the Node.js world. This package is a client that connects to any Socket.IO version 4 server; it has no server of its own. Dial connects, On sets a listener for an event and the Emit... functions send events. Listeners are called while the program processes messages with Run, RunFor, Pump or Next.

The arguments of every event are a JSON array, read with ArgStr, ArgInt and the like. If the sender wants an answer, the event's ack_id is not negative and you answer it with Reply. For several namespaces on one connection, create a Manager.

Types

Endpoint

struct Endpoint
A parsed server URL.
NameTypeDescription
securebool = falseHTTPS or WSS.
hoststr = "localhost"The host.
portint = 80The port; 80 or 443 if it is not given.
pathstr = "/socket.io"The Socket.IO path.
namespacestr = "/"The namespace, from the URL path.
querystr = ""The query.
okbool = falseThe URL is valid.
net-socketio-URL.salam
import net.socketio

func show(url: str):
    e := socketio.ParseURL(url)
    if not e.ok:
        println url, "invalid"
        ret
    end
    println e.host, e.port, e.secure, e.namespace, "query=" + e.query
    println socketio.Origin(e)
end

func main:
    show("http://localhost:3000")
    show("https://example.com/admin/?token=abc")
    show("ws://[::1]:8080/chat")
    show("ftp://example.com")
end
Outputlocalhost 3000 false / query= http://localhost:3000 example.com 443 true /admin query=token=abc https://example.com ::1 8080 false /chat query= http://[::1]:8080 ftp://example.com invalid

Options

struct Options
Connection options.
NameTypeDescription
pathstr = "/socket.io"The path of the Socket.IO server.
querystr = ""An extra query for the URL.
authstr = ""Authentication data as JSON, sent when connecting to the namespace.
headersstr = ""Extra HTTP headers, one per line.
transportsintThe transport, one of the TRANSPORT_... constants.
reconnectionbool = trueReconnect automatically after a disconnect.
reconnection_attemptsint = 0The most attempts; zero means no limit.
reconnection_delayint = 1000The delay before the first attempt in milliseconds; it doubles every time.
reconnection_delay_maxint = 5000The longest delay.
randomization_factorint = 50A random percentage added to or taken from the delay so that all clients do not come back at the same moment.
timeoutint = 20000The connect timeout in milliseconds.
insecurebool = falseDo not check the TLS certificate; for testing only.
auto_connectbool = trueThe client connects right away.
event_queueint = 256The most events in the queue.
ack_timeoutint = 0The default acknowledgement timeout.
retriesint = 0How many times to resend messages that got no acknowledgement.
trailing_slashbool = trueAdd a slash to the end of the path, like the JavaScript client.
remember_upgradebool = falseIf the upgrade to WebSocket worked once, use WebSocket directly next time.
net-socketio-Options.salam
import net.socketio

func main:
    mut o := socketio.NewOptions()
    println o.path, o.reconnection, o.reconnection_delay, o.timeout
    println o.transports == socketio.TRANSPORT_AUTO
    o.transports = socketio.TRANSPORT_WEBSOCKET
    o.auth = `{"token":"abc"}`
    o.auto_connect = false
    println o.transports, o.auth, o.auto_connect
    println socketio.TRANSPORT_POLLING, socketio.REASON_SERVER_DISCONNECT, socketio.REASON_PING_TIMEOUT
    println socketio.PUMP_OK, socketio.PUMP_TIMEOUT, socketio.PUMP_CLOSED
end
Output/socket.io true 1000 20000 true 1 {"token":"abc"} false 2 io server disconnect ping timeout 0 1 2

Client

struct Client
One socket on one namespace.
NameTypeDescription
pvoid*The internal pointer.
net-socketio-Client.salam
import net.socketio
import net.websocket
import str

mut server := websocket.Server {}
mut report := ""

func test_server():
    mut conn := websocket.Accept(server)
    _ := websocket.SendText(conn, `0{"sid":"s1","upgrades":[],"pingInterval":20000,"pingTimeout":5000}`)
    mut going := true
    until going:
        m := websocket.Poll(conn, 3000)
        if m.kind != websocket.TEXT:
            break
        end
        packet := m.text.substr(1, m.text.len() - 1)
        if packet == "0":
            _ := websocket.SendText(conn, `40{"sid":"a1"}`)
        else str.StartsWith(packet, `2["chat"`):
            _ := websocket.SendText(conn, `42["chat","welcome"]`)
            _ := websocket.SendText(conn, `429["question","2+2"]`)
        else str.Contains(packet, `"add"`):
            id := packet.substr(1, str.Find(packet, "[") - 1)
            _ := websocket.SendText(conn, "43" + id + "[5]")
        else str.StartsWith(packet, "39"):
            report += "reply: " + packet.substr(2, packet.len() - 2)
        else packet == "1":
            going = false
        end
    end
    websocket.Close(conn)
end

func main:
    server = websocket.Listen(0)
    t := spawn(test_server)
    mut o := socketio.NewOptions()
    o.transports = socketio.TRANSPORT_WEBSOCKET
    o.reconnection = false
    c := socketio.DialWith("http://127.0.0.1:" + websocket.Port(server), o)
    println socketio.IsConnected(c), socketio.Sid(c), socketio.Id(c), socketio.Namespace(c), socketio.TransportName(c)

    socketio.On(c, "chat", (e: socketio.Event):
        println "message:", socketio.ArgStr(e, 0)
    end)
    socketio.On(c, "question", (e: socketio.Event):
        println "question:", socketio.ArgStr(e, 0), e.ack_id >= 0
        _ := socketio.ReplyStr(e, "four")
    end)
    println socketio.ListenerCount(c, "chat"), socketio.HasListeners(c, "news")
    _ := socketio.EmitStr(c, "chat", "hello")
    socketio.RunFor(c, 500)

    mut a := socketio.NewArgs()
    socketio.AddInt(a, 2)
    socketio.AddInt(a, 3)
    mut k := socketio.EmitAck(c, "add", a, 2000)
    println k.ok, socketio.AckInt(k, 0)
    socketio.AckFree(k)
    socketio.ArgsFree(a)

    socketio.Close(c)
    join(t)
    websocket.CloseServer(server)
    println report
end
Outputtrue s1 a1 / websocket 1 false message: welcome question: 2+2 true true 5 reply: ["four"]

Manager

struct Manager
A connection that several namespaces work on.
NameTypeDescription
pvoid*The internal pointer.
net-socketio-Manager.salam
import net.socketio

func main:
    mut o := socketio.NewOptions()
    o.auth = `{"token":"abc"}`
    o.auto_connect = false
    m := socketio.NewManager("https://example.com", o)
    socketio.SetReconnection(m, true)
    socketio.SetReconnectionAttempts(m, 5)
    socketio.SetReconnectionDelay(m, 500)
    socketio.SetReconnectionDelayMax(m, 10000)
    socketio.SetRandomizationFactor(m, 20)
    socketio.SetTimeout(m, 5000)
    println socketio.OptionsOf(m).reconnection_attempts

    public := socketio.Of(m, "/")
    admin := socketio.Of(m, "/admin")
    socketio.SetAuth(admin, `{"role":"admin"}`)
    println socketio.AuthOf(admin), socketio.Sockets(m).len()
    if not socketio.Open(m):
        println "error:", socketio.ManagerError(m)
        ret
    end
    println socketio.IsOpen(m)
    _ := socketio.Connect(public)
    _ := socketio.Connect(admin)
    if socketio.Refused(admin):
        println "refused:", socketio.RefusalReason(admin)
    end
    println socketio.Active(public), socketio.Disconnected(public), socketio.Recovered(public)
    println socketio.Transport(public), socketio.PingInterval(public), socketio.PingTimeout(public), socketio.MaxPayload(public)
    println socketio.Buffered(public), socketio.Pending(public), socketio.Queued(public), socketio.Attempts(public)
    println socketio.EndpointOf(public).host, socketio.ManagerOf(public).p == m.p
    _ := socketio.PumpManager(m, 1000)
    socketio.Disconnect(admin)
    socketio.CloseManager(m)
end

Event

struct Event
An event that arrived.
NameTypeDescription
kindintThe kind of event.
namestr = ""The event name.
nspstr = "/"The namespace.
datastr = "[]"The arguments as a JSON array.
ack_idint = -1The acknowledgement number if the sender wants one; otherwise -1.
messagestr = ""A description, for error and disconnect events.
binsVectorThe binary parts.
clientClientThe socket that received the event.

The example for this item is shown with the ones above: net-socketio-Client.salam

Ack

struct Ack
An acknowledgement reply.
NameTypeDescription
okbool = falseThe reply arrived before the timeout.
idint = -1The acknowledgement number.
datastr = "[]"The arguments as a JSON array.
binsVectorThe binary parts.

The example for this item is shown with the ones above: net-socketio-Client.salam

Bytes

struct Bytes
The bytes of one binary part.
NameTypeDescription
ptrvoid*The pointer.
lenint = 0The length.
net-socketio-Bytes.salam
import net.socketio

func main:
    mut b := socketio.BytesFromStr("Salam")
    println b.len, socketio.BytesAt(b, 0)
    println socketio.BytesToStr(b)
    println socketio.BytesToBase64(b)
    socketio.BytesFree(b)
    mut c := socketio.NewBytes("hello" as void*, 4)
    println socketio.BytesToStr(c)
    socketio.BytesFree(c)

    mut a := socketio.NewArgs()
    socketio.AddStr(a, "file.txt")
    socketio.AddBinaryStr(a, "abc")
    println socketio.ArgsJSON(a, "upload")
    println a.bins.len()
    socketio.ArgsFree(a)
end
Output5 83 Salam U2FsYW0= hell ["upload","file.txt",{"_placeholder":true,"num":0}] 1

Packet

struct Packet
One Socket.IO packet.
NameTypeDescription
kindint = -1The kind.
nspstr = "/"The namespace.
idint = -1The acknowledgement number; -1 means no acknowledgement.
datastr = ""The data as JSON.
attachmentsint = 0The number of binary parts.
okbool = falseThe packet was read correctly.
net-socketio-Packet.salam
import net.socketio

func main:
    mut p := socketio.Packet {}
    p.kind = socketio.PKT_EVENT
    p.nsp = "/chat"
    p.id = 7
    p.data = `["hi",1]`
    text := socketio.EncodePacket(p)
    println text
    d := socketio.DecodePacket(text)
    println d.ok, d.kind, d.nsp, d.id, d.data
    bad := socketio.DecodePacket("x")
    println bad.ok

    payload := socketio.EncodeEngine(socketio.EIO_MESSAGE, text)
    println payload
    println socketio.EngineKind(payload) == socketio.EIO_MESSAGE
    println socketio.EngineBody(payload)

    mut items := Vector {} as Vector<str>
    items.push("2")
    items.push(payload)
    body := socketio.JoinPayloads(items)
    parts := socketio.SplitPayloads(body)
    println parts.len(), parts.get(0), parts.get(1)

    arr := socketio.SplitJSONArray(`["a", {"b":[1,2]}, 3]`)
    println arr.len(), arr.get(1)

    ph := socketio.Placeholder(0)
    println ph
    println socketio.PlaceholderNum(ph), socketio.PlaceholderNum("12")
end
Output2/chat,7["hi",1] true 2 /chat 7 ["hi",1] false 42/chat,7["hi",1] true 2/chat,7["hi",1] 2 2 42/chat,7["hi",1] 3 {"b":[1,2]} {"_placeholder":true,"num":0} 0 -1

Args

struct Args
The arguments of one message.
NameTypeDescription
itemsVectorThe arguments as JSON.
binsVectorThe binary parts.
net-socketio-Args.salam
import net.socketio
import encoding.value

func main:
    mut a := socketio.NewArgs()
    socketio.AddStr(a, "hello \"world\"")
    socketio.AddInt(a, 42)
    socketio.AddFloat(a, 2.5)
    socketio.AddBool(a, true)
    socketio.AddNull(a)
    socketio.AddJSON(a, `{"x":1}`)
    println socketio.ArgsCount(a)
    text := socketio.ArgsJSON(a, "message")
    println text
    println socketio.ItemCount(text)
    println socketio.ItemStr(text, 1)
    println socketio.ItemInt(text, 2)
    println socketio.ItemFloat(text, 3)
    println socketio.ItemBool(text, 4)
    println socketio.Item(text, 6)
    println socketio.Item(text, 9).len()
    socketio.ArgsFree(a)

    mut v := socketio.ItemValue(text, 2)
    println value.IsNumber(v), value.AsInt(v)
    mut a2 := socketio.NewArgs()
    socketio.AddValue(a2, v)
    socketio.AddBytes(a2, "xyz" as void*, 3)
    println socketio.ArgsJSON(a2, "")
    socketio.ArgsFree(a2)
    value.ValueFree(v)
end
Output6 ["message","hello \"world\"",42,2.5,true,null,{"x":1}] 7 hello "world" 42 2.5 true {"x":1} 0 true 42 [42,{"_placeholder":true,"num":0}]

Constants and variables

TRANSPORT_AUTO

const TRANSPORT_AUTO: i32 = 0
Transport: HTTP polling first, then an upgrade to WebSocket if the server allows it.

The example for this item is shown with the ones above: net-socketio-Options.salam

TRANSPORT_WEBSOCKET

const TRANSPORT_WEBSOCKET: i32 = 1
Transport: WebSocket only.

The example for this item is shown with the ones above: net-socketio-Client.salam

TRANSPORT_POLLING

const TRANSPORT_POLLING: i32 = 2
Transport: HTTP polling only.

The example for this item is shown with the ones above: net-socketio-Options.salam

Event kinds EV_…

The value of the kind field in the event that Next returns. A message event is one of the server's messages; the rest report on the connection itself: connecting and disconnecting, errors, reconnection attempts, pings, timeouts and the upgrade to WebSocket. Listeners are called with the English names of these events, such as connect and disconnect.
NameValue
EV_NONE0
EV_EVENT1
EV_CONNECT2
EV_DISCONNECT3
EV_CONNECT_ERROR4
EV_ERROR5
EV_RECONNECT6
EV_RECONNECT_ATTEMPT7
EV_RECONNECT_ERROR8
EV_RECONNECT_FAILED9
EV_PING10
EV_TIMEOUT11
EV_UPGRADE12
net-socketio-Ack.salam
import net.socketio
import encoding.value

func main:
    c := socketio.Dial("http://localhost:3000")
    if not socketio.IsConnected(c):
        println socketio.LastError(c)
        ret
    end
    socketio.OnAny(c, (e: socketio.Event):
        println "received:", e.name, socketio.ArgCount(e)
    end)
    socketio.Once(c, "welcome", (e: socketio.Event):
        println socketio.Arg(e, 0)
    end)
    socketio.OnAnyOutgoing(c, (e: socketio.Event):
        println "sent:", e.name
    end)

    socketio.PrependAny(c, (e: socketio.Event):
        println "first of all:", e.name
    end)
    socketio.PrependAnyOutgoing(c, (e: socketio.Event):
        println "sending:", e.name
    end)
    println socketio.AnyListenerCount(c)

    _ := socketio.Emit(c, "ping")
    _ := socketio.EmitInt(c, "score", 12)
    _ := socketio.EmitFloat(c, "temp", 21.5)
    _ := socketio.EmitBool(c, "ready", true)
    _ := socketio.EmitJSON(c, "user", `{"name":"Ali"}`)
    _ := socketio.EmitBytes(c, "file", "abc" as void*, 3)
    _ := socketio.Send(c, "a plain message")

    mut a := socketio.NewArgs()
    socketio.AddStr(a, "room-1")
    _ := socketio.EmitArgs(c, "join", a)
    _ := socketio.EmitVolatile(c, "cursor", a)
    _ := socketio.EmitWithAck(c, "join", a, 2000, (k: socketio.Ack):
        println k.ok, socketio.AckCount(k), socketio.AckStr(k, 0)
        println socketio.AckArg(k, 1), socketio.AckFloat(k, 1), socketio.AckBool(k, 2)
        if socketio.AckIsBinary(k, 3):
            println socketio.AckBytes(k, 3).len
        end
        mut v := socketio.AckValue(k, 0)
        value.ValueFree(v)
    end)
    socketio.ArgsFree(a)

    mut e := socketio.Next(c, 5000)
    if e.kind == socketio.EV_EVENT:
        println socketio.ArgInt(e, 0), socketio.ArgFloat(e, 1), socketio.ArgBool(e, 2)
        if socketio.ArgIsBinary(e, 3):
            b := socketio.ArgBytes(e, 3)
            println b.len
        end
        if e.ack_id >= 0:
            _ := socketio.ReplyJSON(e, `{"ok":true}`)
        end
    else e.kind == socketio.EV_DISCONNECT:
        println "disconnected:", e.message
    end
    socketio.EventFree(e)

    mut e2 := socketio.NextEvent(c, 5000)
    mut v2 := socketio.ArgValue(e2, 0)
    println value.IsNumber(v2)
    value.ValueFree(v2)
    socketio.EventFree(e2)

    _ := socketio.Pump(c, 100)
    socketio.Off(c, "welcome")
    socketio.OffAny(c)
    socketio.OffAnyOutgoing(c)
    socketio.OffAll(c)
    socketio.Run(c)
end

Disconnect reasons REASON_…

The text in the message field of a disconnect event, telling why the connection ended: the server or the program itself closed it, a ping did not arrive in time, the transport closed or failed, or a packet could not be read.
NameValue
REASON_SERVER_DISCONNECT"io server disconnect"
REASON_CLIENT_DISCONNECT"io client disconnect"
REASON_PING_TIMEOUT"ping timeout"
REASON_TRANSPORT_CLOSE"transport close"
REASON_TRANSPORT_ERROR"transport error"
REASON_PARSE_ERROR"parse error"

The example for this item is shown with the ones above: net-socketio-Options.salam

Pump results PUMP_…

The result of Pump and PumpManager: something arrived and was handled, the timeout ran out, or the connection is closed.
NameValue
PUMP_OK0
PUMP_TIMEOUT1
PUMP_CLOSED2

The example for this item is shown with the ones above: net-socketio-Options.salam

Engine packet kinds EIO_…

Socket.IO runs on a layer called Engine.IO. Every message on that layer starts with a digit that gives its kind: open, close, ping, pong, message, upgrade and noop. Socket.IO packets travel inside messages of the "message" kind.
NameValue
EIO_OPEN0
EIO_CLOSE1
EIO_PING2
EIO_PONG3
EIO_MESSAGE4
EIO_UPGRADE5
EIO_NOOP6

The example for this item is shown with the ones above: net-socketio-Packet.salam

Socket.IO packet kinds PKT_…

The kinds of Socket.IO packet: connecting to a namespace, disconnecting, an event, an acknowledgement, a connect error, and the binary event and binary acknowledgement whose bytes follow the packet separately.
NameValue
PKT_CONNECT0
PKT_DISCONNECT1
PKT_EVENT2
PKT_ACK3
PKT_CONNECT_ERROR4
PKT_BINARY_EVENT5
PKT_BINARY_ACK6

The example for this item is shown with the ones above: net-socketio-Packet.salam

Functions

ParseURL

func ParseURL(raw: str): Endpoint
Parses a URL; the http, https, ws and wss schemes are accepted.

The example for this item is shown with the ones above: net-socketio-URL.salam

Origin

func Origin(e: Endpoint): str
The origin of the URL, such as https://example.com; the default port is left out.

The example for this item is shown with the ones above: net-socketio-URL.salam

EncodePacket

func EncodePacket(p: Packet): str
Turns a packet into protocol text.

The example for this item is shown with the ones above: net-socketio-Packet.salam

DecodePacket

func DecodePacket(text: str): Packet
Turns text back into a packet; if it is invalid, ok is false.

The example for this item is shown with the ones above: net-socketio-Packet.salam

EncodeEngine

func EncodeEngine(kind: int, body: str): str
An Engine.IO message with a kind and a body.

The example for this item is shown with the ones above: net-socketio-Packet.salam

EngineKind

func EngineKind(payload: str): int
The kind of an Engine.IO message.

The example for this item is shown with the ones above: net-socketio-Packet.salam

EngineBody

func EngineBody(payload: str): str
The body of an Engine.IO message, without the kind digit.

The example for this item is shown with the ones above: net-socketio-Packet.salam

SplitPayloads

func SplitPayloads(body: str): Vector<str>
Splits a polling response into messages; the separator is character 30.

The example for this item is shown with the ones above: net-socketio-Packet.salam

JoinPayloads

func JoinPayloads(items: Vector<str>): str
Joins messages together for one polling request.

The example for this item is shown with the ones above: net-socketio-Packet.salam

SplitJSONArray

func SplitJSONArray(text: str): Vector<str>
Splits a JSON array into the text of its top level elements.

The example for this item is shown with the ones above: net-socketio-Packet.salam

Placeholder

func Placeholder(num: int): str
The placeholder for the binary part with this number.

The example for this item is shown with the ones above: net-socketio-Packet.salam

PlaceholderNum

func PlaceholderNum(frag: str): int
The number of a placeholder; -1 if it is not a placeholder.

The example for this item is shown with the ones above: net-socketio-Packet.salam

NewBytes

func NewBytes(ptr: void*, n: int): Bytes
Copies bytes.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

BytesFromStr

func BytesFromStr(s: str): Bytes
The bytes of a string.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

BytesToStr

func BytesToStr(b: Bytes): str
The bytes as a string.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

BytesToBase64

func BytesToBase64(b: Bytes): str
The bytes as base64.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

BytesAt

func BytesAt(b: Bytes, i: int): int
One byte, from 0 to 255.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

BytesFree

func BytesFree(b &: Bytes)
Frees the memory.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

NewArgs

func NewArgs(): Args
An empty argument list.

The example for this item is shown with the ones above: net-socketio-Client.salam

AddStr

func AddStr(a &: Args, s: str)
Adds a string.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddInt

func AddInt(a &: Args, n: i64)
An integer.

The example for this item is shown with the ones above: net-socketio-Client.salam

AddFloat

func AddFloat(a &: Args, n: f64)
A float.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddBool

func AddBool(a &: Args, b: bool)
A boolean.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddNull

func AddNull(a &: Args)
A null.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddJSON

func AddJSON(a &: Args, raw: str)
Ready JSON text; an empty string becomes null.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddValue

func AddValue(a &: Args, v: value.Value)
An encoding value.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddBytes

func AddBytes(a &: Args, ptr: void*, n: int)
Sends the bytes separately and puts a placeholder in their place.

The example for this item is shown with the ones above: net-socketio-Args.salam

AddBinaryStr

func AddBinaryStr(a &: Args, s: str)
Sends the bytes of a string as binary.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

ArgsCount

func ArgsCount(a: Args): int
The number of arguments.

The example for this item is shown with the ones above: net-socketio-Args.salam

ArgsFree

func ArgsFree(a &: Args)
Frees the memory.

The example for this item is shown with the ones above: net-socketio-Client.salam

ArgsJSON

func ArgsJSON(a: Args, name: str): str
The JSON array that is sent; the event name, if not empty, comes first.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemCount

func ItemCount(raw: str): int
The number of elements in a JSON array.

The example for this item is shown with the ones above: net-socketio-Args.salam

Item

func Item(raw: str, i: int): str
The JSON text of one element; an empty string if there is none.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemStr

func ItemStr(raw: str, i: int): str
The element as a string.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemInt

func ItemInt(raw: str, i: int): i64
The element as an integer; a numeric string is read too.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemFloat

func ItemFloat(raw: str, i: int): f64
The element as a float.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemBool

func ItemBool(raw: str, i: int): bool
The element as a boolean; a nonzero number and the string true count as true.

The example for this item is shown with the ones above: net-socketio-Args.salam

ItemValue

func ItemValue(raw: str, i: int): value.Value
The element as an encoding value.

The example for this item is shown with the ones above: net-socketio-Args.salam

NewOptions

func NewOptions(): Options
The default options.

The example for this item is shown with the ones above: net-socketio-Client.salam

NewManager

func NewManager(url: str, opts: Options): Manager
A new manager for a URL.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Of

func Of(m: Manager, nsp: str): Client
The socket for a namespace on the manager.

The example for this item is shown with the ones above: net-socketio-Bytes.salam

ManagerOf

func ManagerOf(c: Client): Manager
The manager the socket is on.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Dial

func Dial(url: str): Client
Connects to a URL; the URL path is the namespace. If it does not connect, see why with LastError.

The example for this item is shown with the ones above: net-socketio-Client.salam

DialWith

func DialWith(url: str, opts: Options): Client
Like the function above, with options.

The example for this item is shown with the ones above: net-socketio-Client.salam

Connect

func Connect(c: Client): bool
Connects the socket; if it was connected before, connects it again.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Disconnect

func Disconnect(c: Client)
Leaves the namespace; no automatic reconnection follows.

The example for this item is shown with the ones above: net-socketio-Manager.salam

CloseManager

func CloseManager(m: Manager)
Closes the connection and all its sockets.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Close

func Close(c: Client)
Closes the socket.

The example for this item is shown with the ones above: net-socketio-Client.salam

Pump

func Pump(c: Client, timeout_ms: int): int
Handles the messages that arrive within this timeout and calls the listeners.

The example for this item is shown with the ones above: net-socketio-Options.salam

PumpManager

func PumpManager(m: Manager, timeout_ms: int): int
Like the function above, for all the manager's sockets.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Next

func Next(c: Client, timeout_ms: int): Event
The next event from the queue, or an event of kind EV_NONE if nothing arrived within the timeout. Listeners are called too.

The example for this item is shown with the ones above: net-socketio-Ack.salam

NextEvent

func NextEvent(c: Client, timeout_ms: int): Event
Like the function above, but only message events.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Run

func Run(c: Client)
Handles messages until the socket closes.

The example for this item is shown with the ones above: net-socketio-Client.salam

RunFor

func RunFor(c: Client, ms: int)
Handles messages for this many milliseconds.

The example for this item is shown with the ones above: net-socketio-Client.salam

On

func On(c: Client, name: str, cb: func(Event))
Sets a listener for an event.

The example for this item is shown with the ones above: net-socketio-Client.salam

Once

func Once(c: Client, name: str, cb: func(Event))
A listener that is called only once.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Off

func Off(c: Client, name: str)
Removes the listeners of an event.

The example for this item is shown with the ones above: net-socketio-Ack.salam

OffAll

func OffAll(c: Client)
Removes all listeners.

The example for this item is shown with the ones above: net-socketio-Ack.salam

OnAny

func OnAny(c: Client, cb: func(Event))
A listener for every message event.

The example for this item is shown with the ones above: net-socketio-Ack.salam

OffAny

func OffAny(c: Client)
Removes the listeners for every event.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitArgs

func EmitArgs(c: Client, name: str, a: Args): bool
Sends an event with arguments. If not connected, the message waits in the queue. Reserved names, such as connect, are refused.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitVolatile

func EmitVolatile(c: Client, name: str, a: Args): bool
Like the function above, but if not connected, the message is dropped.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Emit

func Emit(c: Client, name: str): bool
An event with no arguments.

The example for this item is shown with the ones above: net-socketio-Client.salam

EmitStr

func EmitStr(c: Client, name: str, s: str): bool
An event with one string.

The example for this item is shown with the ones above: net-socketio-Client.salam

EmitInt

func EmitInt(c: Client, name: str, n: i64): bool
With one integer.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitFloat

func EmitFloat(c: Client, name: str, n: f64): bool
With one float.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitBool

func EmitBool(c: Client, name: str, b: bool): bool
With one boolean.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitJSON

func EmitJSON(c: Client, name: str, raw: str): bool
With one JSON text.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitBytes

func EmitBytes(c: Client, name: str, ptr: void*, n: int): bool
With binary bytes.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EmitAck

func EmitAck(c: Client, name: str, a: Args, timeout_ms: int): Ack
Sends an event and waits until the acknowledgement arrives or the timeout runs out.

The example for this item is shown with the ones above: net-socketio-Client.salam

EmitWithAck

func EmitWithAck(c: Client, name: str, a: Args, timeout_ms: int, cb: func(Ack)): bool
Sends an event and calls the given function when the acknowledgement arrives or the timeout runs out; it does not wait.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Reply

func Reply(ev: Event, a: Args): bool
Answers an event that asked for an acknowledgement.

The example for this item is shown with the ones above: net-socketio-Client.salam

ReplyJSON

func ReplyJSON(ev: Event, raw: str): bool
Answers with one JSON text.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ReplyStr

func ReplyStr(ev: Event, s: str): bool
Answers with one string.

The example for this item is shown with the ones above: net-socketio-Client.salam

ArgCount

func ArgCount(ev: Event): int
The number of arguments of the event.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Arg

func Arg(ev: Event, i: int): str
The JSON text of one argument.

The example for this item is shown with the ones above: net-socketio-Client.salam

ArgStr

func ArgStr(ev: Event, i: int): str
The argument as a string.

The example for this item is shown with the ones above: net-socketio-Client.salam

ArgInt

func ArgInt(ev: Event, i: int): i64
An integer argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ArgFloat

func ArgFloat(ev: Event, i: int): f64
A float argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ArgBool

func ArgBool(ev: Event, i: int): bool
A boolean argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ArgValue

func ArgValue(ev: Event, i: int): value.Value
The argument as an encoding value.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ArgIsBinary

func ArgIsBinary(ev: Event, i: int): bool
The argument is binary.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ArgBytes

func ArgBytes(ev: Event, i: int): Bytes
The bytes of a binary argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckCount

func AckCount(a: Ack): int
The number of acknowledgement arguments.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckArg

func AckArg(a: Ack, i: int): str
The JSON text of one acknowledgement argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckStr

func AckStr(a: Ack, i: int): str
The acknowledgement argument as a string.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckInt

func AckInt(a: Ack, i: int): i64
As an integer.

The example for this item is shown with the ones above: net-socketio-Client.salam

AckFloat

func AckFloat(a: Ack, i: int): f64
As a float.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckBool

func AckBool(a: Ack, i: int): bool
As a boolean.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckIsBinary

func AckIsBinary(a: Ack, i: int): bool
The acknowledgement argument is binary.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckBytes

func AckBytes(a: Ack, i: int): Bytes
The bytes of a binary acknowledgement argument.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckValue

func AckValue(a: Ack, i: int): value.Value
As an encoding value.

The example for this item is shown with the ones above: net-socketio-Ack.salam

EventFree

func EventFree(ev &: Event)
Frees the event's memory.

The example for this item is shown with the ones above: net-socketio-Ack.salam

AckFree

func AckFree(a &: Ack)
Frees the acknowledgement's memory.

The example for this item is shown with the ones above: net-socketio-Client.salam

IsConnected

func IsConnected(c: Client): bool
The socket is connected.

The example for this item is shown with the ones above: net-socketio-Client.salam

Id

func Id(c: Client): str
The socket's id in the namespace.

The example for this item is shown with the ones above: net-socketio-Client.salam

Sid

func Sid(c: Client): str
The Engine.IO session id.

The example for this item is shown with the ones above: net-socketio-Client.salam

Namespace

func Namespace(c: Client): str
The namespace.

The example for this item is shown with the ones above: net-socketio-Client.salam

Recovered

func Recovered(c: Client): bool
After a reconnect, the earlier state was recovered.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Transport

func Transport(c: Client): int
The current transport.

The example for this item is shown with the ones above: net-socketio-Manager.salam

LastError

func LastError(c: Client): str
The last error.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Buffered

func Buffered(c: Client): int
The number of messages waiting for a connection.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Attempts

func Attempts(c: Client): int
The number of reconnection attempts.

The example for this item is shown with the ones above: net-socketio-Manager.salam

EndpointOf

func EndpointOf(c: Client): Endpoint
The parsed URL of the socket.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Send

func Send(c: Client, msg: str): bool
A message event with one string; like send in JavaScript.

The example for this item is shown with the ones above: net-socketio-Ack.salam

PrependAny

func PrependAny(c: Client, cb: func(Event))
Like OnAny, but called before the others.

The example for this item is shown with the ones above: net-socketio-Ack.salam

OnAnyOutgoing

func OnAnyOutgoing(c: Client, cb: func(Event))
A listener for every message that is sent.

The example for this item is shown with the ones above: net-socketio-Ack.salam

PrependAnyOutgoing

func PrependAnyOutgoing(c: Client, cb: func(Event))
Like the function above, before the others.

The example for this item is shown with the ones above: net-socketio-Ack.salam

OffAnyOutgoing

func OffAnyOutgoing(c: Client)
Removes the listeners for outgoing messages.

The example for this item is shown with the ones above: net-socketio-Ack.salam

ListenerCount

func ListenerCount(c: Client, name: str): int
The number of listeners for an event.

The example for this item is shown with the ones above: net-socketio-Client.salam

HasListeners

func HasListeners(c: Client, name: str): bool
The event has listeners.

The example for this item is shown with the ones above: net-socketio-Client.salam

AnyListenerCount

func AnyListenerCount(c: Client): int
The number of listeners for every event.

The example for this item is shown with the ones above: net-socketio-Ack.salam

Disconnected

func Disconnected(c: Client): bool
The socket is disconnected.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Active

func Active(c: Client): bool
The socket is active and reconnects automatically if it drops.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Pending

func Pending(c: Client): int
The number of acknowledgements that have not arrived yet.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Queued

func Queued(c: Client): int
The number of unread events in the queue.

The example for this item is shown with the ones above: net-socketio-Manager.salam

TransportName

func TransportName(c: Client): str
The transport name: websocket or polling.

The example for this item is shown with the ones above: net-socketio-Client.salam

PingInterval

func PingInterval(c: Client): int
The ping interval the server set.

The example for this item is shown with the ones above: net-socketio-Manager.salam

PingTimeout

func PingTimeout(c: Client): int
The ping timeout.

The example for this item is shown with the ones above: net-socketio-Options.salam

MaxPayload

func MaxPayload(c: Client): int
The largest message size the server accepts.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetAuth

func SetAuth(c: Client, auth json: str)
Changes the authentication data for later connections.

The example for this item is shown with the ones above: net-socketio-Manager.salam

AuthOf

func AuthOf(c: Client): str
The authentication data.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetReconnection

func SetReconnection(m: Manager, enabled: bool)
Turns automatic reconnection on or off.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetReconnectionAttempts

func SetReconnectionAttempts(m: Manager, n: int)
The most attempts.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetReconnectionDelay

func SetReconnectionDelay(m: Manager, ms: int)
The delay before the first attempt.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetReconnectionDelayMax

func SetReconnectionDelayMax(m: Manager, ms: int)
The longest delay.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetRandomizationFactor

func SetRandomizationFactor(m: Manager, percent: int)
The random percentage of the delay.

The example for this item is shown with the ones above: net-socketio-Manager.salam

SetTimeout

func SetTimeout(m: Manager, ms: int)
The connect timeout.

The example for this item is shown with the ones above: net-socketio-Manager.salam

OptionsOf

func OptionsOf(m: Manager): Options
The manager's current options.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Sockets

func Sockets(m: Manager): Vector<Client>
The manager's sockets.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Open

func Open(m: Manager): bool
Opens the manager's connection.

The example for this item is shown with the ones above: net-socketio-Manager.salam

IsOpen

func IsOpen(m: Manager): bool
The manager's connection is open.

The example for this item is shown with the ones above: net-socketio-Manager.salam

ManagerError

func ManagerError(m: Manager): str
The manager's last error.

The example for this item is shown with the ones above: net-socketio-Manager.salam

Refused

func Refused(c: Client): bool
The server refused the connection to the namespace, for example because the authentication was wrong.

The example for this item is shown with the ones above: net-socketio-Manager.salam

RefusalReason

func RefusalReason(c: Client): str
The reason the server gave.

The example for this item is shown with the ones above: net-socketio-Manager.salam