Web Sockets
Use negotiate.WsUpgrade() to upgrade the connection to web sockets. package main
import (
"io"
"main/lib/core/negotiate"
"main/lib/core/routes"
"main/lib/core/scopes"
"net/http"
"time"
)
var _ = routes.Route{
Pattern: "GET /",
Handler: func(_ scopes.Scope, request *http.Request, writer http.ResponseWriter) {
negotiate.WsUpgrade(&writer, request) // Negotiates ws upgrade with the client
// and replaces writer and request with
// ws compliant ones.
for {
_, _ = io.ReadAll(request.Body) // Receives message.
_, _ = writer.Write([]byte("hello")) // Sends message.
time.Sleep(time.Second) // Sleeps for 1 second.
}
},
} Then consume the web socket on the client. <script lang="ts">
const messages: string[] = $state([]) // Creates reactive list of messages.
const socket = new WebSocket("/ws") // Connects to handler.
socket.addEventListener("message", function listen(event:MessageEvent) {
messages.push(event.data) // Appends incoming messages to the
// reactive list of messages for later use.
})
socket.send("Hello") // Sends message.
</script>
<Title text="Messages"/>
{#each messages as message, id (id)} <!-- Iterates the list of messages. -->
<div>{message}</div> <!-- Renders message. -->
{/each}