websocket

package
v0.0.0-...-90c9d3a Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2010 License: BSD-3-Clause, GooglePatentClause Imports: 5 Imported by: 0

Documentation

Overview

The websocket package 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 (
	ErrBadStatus            = &ProtocolError{"bad status"}
	ErrNoUpgrade            = &ProtocolError{"no upgrade"}
	ErrBadUpgrade           = &ProtocolError{"bad upgrade"}
	ErrNoWebSocketOrigin    = &ProtocolError{"no WebSocket-Origin"}
	ErrBadWebSocketOrigin   = &ProtocolError{"bad WebSocket-Origin"}
	ErrNoWebSocketLocation  = &ProtocolError{"no WebSocket-Location"}
	ErrBadWebSocketLocation = &ProtocolError{"bad WebSocket-Location"}
	ErrNoWebSocketProtocol  = &ProtocolError{"no WebSocket-Protocol"}
	ErrBadWebSocketProtocol = &ProtocolError{"bad WebSocket-Protocol"}
)

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
	// 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 is:

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

SeWritetTimeout 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 Handler

type Handler func(*Conn)

Handler is an interface to a WebSocket. A trivial example server is:

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(c *http.Conn, 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