Documentation ¶
Overview ¶
Copyright 2011 Chris Kowalik (nu7hatch). All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package webrocket implements advanced WebSocket server with custom protocols support.
Index ¶
Constants ¶
const ( ReadOnlyAccess = "read-only" ReadWriteAccess = "read-write" )
Access control constants.
Variables ¶
var AccessCodes map[string]int = map[string]int{
ReadOnlyAccess: 1,
ReadWriteAccess: 2,
}
Access control defaults.
Functions ¶
func NewHandler ¶
Creates new handler based on specified websocket's codec. Here's an trivial example:
server := webrocket.NewServer("localhost:8080") handler := webrocket.NewHandler(websocket.JSON) server.Handle("/echo", handler) server.ListenAndServe()
func NewJSONHandler ¶
func NewJSONHandler() *handler
Creates new handler basd on the default JSON protocol.
Types ¶
type Credentials ¶ added in v0.2.0
Storage for credentials setup.
type Data ¶ added in v0.2.0
type Data map[string]interface{}
Data is an general structure for all received event messages.
type Handler ¶
Handler handlers all incoming requestes using defined protocol. Handler also manages all registered channels.
Trivial custom handler:
type MyHandler struct { channels map[string] } func (*h MyHandler) Register(id interface{}) { // initialize your handler here... }
type Payload ¶ added in v0.2.0
type Payload map[string]interface{}
Payload is an general strucutre for all sent event messages.
Simple examples how to create new event message:
Payload SimpleMessage = Payload("hello": "world") Payload ComplexMessage = Payload("hello": Data{"foo": "bar"})
var ( Ok Payload = Payload{"ok": true} InvalidData Payload = Payload{"err": "invalid_data"} InvalidCredentials Payload = Payload{"err": "invalid_credentials"} InvalidChannelName Payload = Payload{"err": "invalid_channel_name"} InvalidChannel Payload = Payload{"err": "invalid_channel"} AccessDenied Payload = Payload{"err": "access_denied"} )
Predefined payloads.
type Server ¶
Server defines parameters for running an WebSocket server.
func NewServer ¶
Creates new rocket's server bound to specified addr. A Trivial example server:
package main import "rocket" func main() { s := rocket.NewServer("ws://localhost:8080") s.Handle("/echo", rocket.NewJSONHandler()) s.ListenAndServe() }
func (*Server) Handle ¶
Registers payload handler under specified path. Handler have to implement communication protocol callbacks.
func (*Server) ListenAndServe ¶
Listens on the TCP network address srv.Addr and handles requests on incoming websocket connections.