websocket

package module
v0.0.0-...-9a42957 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2019 License: BSD-3-Clause Imports: 8 Imported by: 38

README

websocket

GoDoc

Packages websocket and websocketjs provide high- and low-level bindings for the browser's WebSocket API (respectively).

The high-level bindings offer a Dial function that returns a regular net.Conn. It can be used similarly to net package.

conn, err := websocket.Dial("ws://localhost/socket") // Blocks until connection is established.
if err != nil {
	// handle error
}

buf := make([]byte, 1024)
n, err = conn.Read(buf) // Blocks until a WebSocket frame is received.
doSomethingWithData(buf[:n])
if err != nil {
	// handle error
}

_, err = conn.Write([]byte("Hello!"))
// ...

err = conn.Close()
// ...

The low-level bindings work with typical JavaScript idioms, such as adding event listeners with callbacks.

ws, err := websocketjs.New("ws://localhost/socket") // Does not block.
if err != nil {
	// handle error
}

onOpen := func(ev *js.Object) {
	err := ws.Send([]byte("Hello!")) // Send a binary frame.
	// ...
	err := ws.Send("Hello!") // Send a text frame.
	// ...
}

ws.AddEventListener("open", false, onOpen)
ws.AddEventListener("message", false, onMessage)
ws.AddEventListener("close", false, onClose)
ws.AddEventListener("error", false, onError)

err = ws.Close()
// ...

Alternatives

Documentation

Overview

Package websocket provides high-level bindings for the browser's WebSocket API.

These bindings offer a Dial function that returns a regular net.Conn. It can be used similarly to net package.

conn, err := websocket.Dial("ws://localhost/socket") // Blocks until connection is established.
if err != nil {
	// handle error
}

buf := make([]byte, 1024)
n, err = conn.Read(buf) // Blocks until a WebSocket frame is received.
doSomethingWithData(buf[:n])
if err != nil {
	// handle error
}

_, err = conn.Write([]byte("Hello!"))
// ...

err = conn.Close()
// ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(url string) (net.Conn, error)

Dial opens a new WebSocket connection. It will block until the connection is established or fails to connect.

Types

This section is empty.

Notes

Bugs

  • conn.LocalAddr() panics because the underlying JavaScript API has no way of figuring out the local address.

Directories

Path Synopsis
Package websocketjs provides low-level bindings for the browser's WebSocket API.
Package websocketjs provides low-level bindings for the browser's WebSocket API.

Jump to

Keyboard shortcuts

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