package wsjs

import "github.com/coder/websocket/internal/wsjs"

Package wsjs implements typed access to the browser javascript WebSocket API.

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

Index

Types

type CloseEvent

type CloseEvent struct {
	Code     uint16
	Reason   string
	WasClean bool
}

CloseEvent is the type passed to a WebSocket close handler.

type MessageEvent

type MessageEvent struct {
	// string or []byte.
	Data interface{}
}

MessageEvent is the type passed to a message handler.

type WebSocket

type WebSocket struct {
	// contains filtered or unexported fields
}

WebSocket is a wrapper around a javascript WebSocket object.

func New

func New(url string, protocols []string) (c WebSocket, err error)

New is a wrapper around the javascript WebSocket constructor.

func (WebSocket) Close

func (c WebSocket) Close(code int, reason string) (err error)

Close closes the WebSocket with the given code and reason.

func (WebSocket) OnClose

func (c WebSocket) OnClose(fn func(CloseEvent)) (remove func())

OnClose registers a function to be called when the WebSocket is closed.

func (WebSocket) OnError

func (c WebSocket) OnError(fn func(e js.Value)) (remove func())

OnError registers a function to be called when there is an error with the WebSocket.

func (WebSocket) OnMessage

func (c WebSocket) OnMessage(fn func(m MessageEvent)) (remove func())

OnMessage registers a function to be called when the WebSocket receives a message.

func (WebSocket) OnOpen

func (c WebSocket) OnOpen(fn func(e js.Value)) (remove func())

OnOpen registers a function to be called when the WebSocket is opened.

func (WebSocket) SendBytes

func (c WebSocket) SendBytes(v []byte) (err error)

SendBytes sends the given message as a binary message on the WebSocket.

func (WebSocket) SendText

func (c WebSocket) SendText(v string) (err error)

SendText sends the given string as a text message on the WebSocket.

func (WebSocket) Subprotocol

func (c WebSocket) Subprotocol() string

Subprotocol returns the WebSocket subprotocol in use.

Source Files

wsjs_js.go

Version
v1.8.13 (latest)
Published
Mar 14, 2025
Platform
js/wasm
Imports
1 packages
Last checked
1 month ago

Tools for package owners.