gopherws

package module
v0.0.0-...-3f4841a Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: BSD-3-Clause Imports: 9 Imported by: 0

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()
// ...

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

This section is empty.

Types

type Conn

type Conn struct {
	*websocketjs.WebSocket
	// contains filtered or unexported fields
}

conn is a high-level wrapper around WebSocket. It implements net.Conn interface.

func Dial

func Dial(url string) (*Conn, error)

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

func (*Conn) AddSession

func (c *Conn) AddSession(id string, sessionObj interface{}) error

Add session to connection

func (*Conn) GetSeshKey

func (c *Conn) GetSeshKey() string

Get session key

func (*Conn) GetSession

func (c *Conn) GetSession(id string) (interface{}, error)

Return session object

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr would typically return the local network address, but due to limitations in the JavaScript API, it is unable to. Calling this method will cause a panic.

func (*Conn) Read

func (c *Conn) Read(b []byte) (n int, err error)

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address, based on websocket.WebSocket.URL.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A zero value for t means that I/O operations will not time out.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future Read calls. A zero value for t means Read will not time out.

func (*Conn) SetSeshKey

func (c *Conn) SetSeshKey(seshKey string)

Set session key for connection

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future Write calls. Because our writes do not block, this function is a no-op.

func (*Conn) Write

func (c *Conn) Write(b []byte) (n int, err error)

Write writes the contents of b to the WebSocket using a binary opcode.

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