websocket

package
v0.0.0-...-e758773 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2011 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package websocket implements a client and server for the Web Socket protocol. The protocol is defined at http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadScheme            = os.ErrorString("bad scheme")
	ErrBadStatus            = &ProtocolError{"bad status"}
	ErrBadUpgrade           = &ProtocolError{"missing or bad upgrade"}
	ErrBadWebSocketOrigin   = &ProtocolError{"missing or bad WebSocket-Origin"}
	ErrBadWebSocketLocation = &ProtocolError{"missing or bad WebSocket-Location"}
	ErrBadWebSocketProtocol = &ProtocolError{"missing or bad WebSocket-Protocol"}
	ErrChallengeResponse    = &ProtocolError{"mismatch challenge/response"}
)

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// The origin URI for the Web Socket.
	Origin string
	// The location URI for the Web Socket.
	Location string
	// The subprotocol for the Web Socket.
	Protocol string
	// The initial http Request (for the Server side only).
	Request *http.Request
	// contains filtered or unexported fields
}

Conn is a channel to communicate to a Web Socket. It implements the net.Conn interface.

func Dial

func Dial(url, protocol, origin string) (ws *Conn, err os.Error)

Dial opens a new client connection to a Web Socket.

A trivial example client:

package main

import (
	"websocket"
	"strings"
)

func main() {
 	ws, err := websocket.Dial("ws://localhost/ws", "", "http://localhost/");
 	if err != nil {
		panic("Dial: " + err.String())
	}
	if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
		panic("Write: " + err.String())
	}
	var msg = make([]byte, 512);
	if n, err := ws.Read(msg); err != nil {
		panic("Read: " + err.String())
	}
	// use msg[0:n]
}

func (*Conn) Close

func (ws *Conn) Close() os.Error

Close implements the io.Closer interface for a Conn.

func (*Conn) LocalAddr

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

LocalAddr returns the WebSocket Origin for the connection.

func (*Conn) Read

func (ws *Conn) Read(msg []byte) (n int, err os.Error)

Read implements the io.Reader interface for a Conn.

func (*Conn) RemoteAddr

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

RemoteAddr returns the WebSocket locations for the connection.

func (*Conn) SetReadTimeout

func (ws *Conn) SetReadTimeout(nsec int64) os.Error

SetReadTimeout sets the connection's network read timeout in nanoseconds.

func (*Conn) SetTimeout

func (ws *Conn) SetTimeout(nsec int64) os.Error

SetTimeout sets the connection's network timeout in nanoseconds.

func (*Conn) SetWriteTimeout

func (ws *Conn) SetWriteTimeout(nsec int64) os.Error

SetWriteTimeout sets the connection's network write timeout in nanoseconds.

func (*Conn) Write

func (ws *Conn) Write(msg []byte) (n int, err os.Error)

Write implements the io.Writer interface for a Conn.

type DialError

type DialError struct {
	URL      string
	Protocol string
	Origin   string
	Error    os.Error
}

func (*DialError) String

func (e *DialError) String() string

type Draft75Handler

type Draft75Handler func(*Conn)

Draft75Handler is an interface to a WebSocket based on the (soon obsolete) draft-hixie-thewebsocketprotocol-75.

func (Draft75Handler) ServeHTTP

func (f Draft75Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface for a Web Socket.

type Handler

type Handler func(*Conn)

Handler is an interface to a WebSocket.

A trivial example server:

package main

import (
	"http"
	"io"
	"websocket"
)

// Echo the data received on the Web Socket.
func EchoServer(ws *websocket.Conn) {
	io.Copy(ws, ws);
}

func main() {
	http.Handle("/echo", websocket.Handler(EchoServer));
	err := http.ListenAndServe(":12345", nil);
	if err != nil {
		panic("ListenAndServe: " + err.String())
	}
}

func (Handler) ServeHTTP

func (f Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface for a Web Socket

type ProtocolError

type ProtocolError struct {
	os.ErrorString
}

type WebSocketAddr

type WebSocketAddr string

WebSocketAddr is an implementation of net.Addr for Web Sockets.

func (WebSocketAddr) Network

func (addr WebSocketAddr) Network() string

Network returns the network type for a Web Socket, "websocket".

func (WebSocketAddr) String

func (addr WebSocketAddr) String() string

String returns the network address for a Web Socket.

Jump to

Keyboard shortcuts

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