websocketutils

package module
v0.0.0-...-6b277e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2023 License: MIT Imports: 9 Imported by: 0

README

websocketutils

This compiles Golang to wasm (webassembly). It opens a websocket back to the server, and then says so on the browser page.

Caveat: It has been tested only on localhost.

GOARCH=wasm GOOS=js go build -o client.wasm client.go
go run ../server/main.go 

Then open http://localhost:9090

The websocket is requested via a call to http://localhost:9090/ws (If you try to load this in a browser, you get weird errors!)

This was developed initially in Google Chrome on macOS using go1.16, and more recently in Safari on macOS using go1.21.4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCtx, ZeroTimeCtx CTX.Context
View Source
var DefaultCtxCancel CTX.CancelFunc

Functions

func SpawnUpgradedWSs

func SpawnUpgradedWSs(w http.ResponseWriter, req *http.Request)

Types

type UpgradedWebsocketHandler

type UpgradedWebsocketHandler func(*WSUConn)
var GlobalUpgradedWebsocketHandler UpgradedWebsocketHandler

type WSUConn

type WSUConn struct {
	*WS.Conn
	NC                    net.Conn
	LocalAddr, RemoteAddr net.Addr
	Protocol, Address     string
}

WSUConn is a WebSocket Utils connection, containing both an embedded [websocket.Conn] and an embedded net.Conn. This makes the API kind of messy - the same verbs appear in different APIs, with different function signatures. Also, we rename one of them so that we don't trigger any weird compiler errors. .

func NewWSUConnForDial

func NewWSUConnForDial(address string) (p *WSUConn, e error)

NewWSUConnForDial sets up fields but does not Dial. Since only a call to [websocket.Dial] (or [websocket.Accept]) actually creates an actual websocket, this func does not yet either create or initialise the embedded websocket (the [*WS.Conn]). .

func (*WSUConn) Dial

func (p *WSUConn) Dial() error

Dial calls the underlying [websocket.Dial] to get an actual websocket. From the API docs:

  • Dial(ctx, u string, opts *DialOpts) (*Conn, *http.Response, error)
  • Dial performs a WebSocket handshake on url.
  • The *http.Response is the WebSocket handshake response from the server, but you never need to close the rsp.Body yourself.
  • If an error occurs, the returned response may be non nil, but you can only read the first 1024 bytes of the body.
  • http:// & https:// URLs work and are interpreted as ws/wss.
  • NOTE: *http.Response from Dial is &http.Response{} with a 101 status code on success
  • NOTE: It specifies a text connection, not a binary connection.

.

func (*WSUConn) Read

func (p *WSUConn) Read() (string, error)

func (*WSUConn) WriteAndRead

func (p *WSUConn) WriteAndRead(s string) (string, error)

func (*WSUConn) WriteAndReturn

func (p *WSUConn) WriteAndReturn(s string) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL