Package websocket
import "websocket"
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
Package files
client.go server.go websocket.goVariables
var (
ErrBadScheme = &ProtocolError{"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"}
)
type Conn
Conn is a channel to communicate to a Web Socket. It implements the net.Conn interface.
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
}
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
Draft75Handler is an interface to a WebSocket based on the (soon obsolete) draft-hixie-thewebsocketprotocol-75.
type Draft75Handler func(*Conn)
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
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())
}
}
type Handler func(*Conn)
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 {
ErrorString string
}
func (*ProtocolError) String
func (err *ProtocolError) String() string
type WebSocketAddr
WebSocketAddr is an implementation of net.Addr for Web Sockets.
type WebSocketAddr string
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.