-
Notifications
You must be signed in to change notification settings - Fork 1k
Embedding in go apps
Alex Sergeyev edited this page May 30, 2014
·
5 revisions
It is possible to embed websocketd
directly inside an existing go project by registering a handler.
Here is an example:
package main
import (
"fmt"
wsd "github.com/joewalnes/websocketd/libwebsocketd"
"net/http"
)
const MAXFORKS = 10
func main() {
// A log scope allows you to customize the logging that websocketd performs.
//You can provide your own log scope with a log func.
logScope := wsd.RootLogScope(wsd.LogAccess, func(l *wsd.LogScope,
level wsd.LogLevel, levelName string,
category string, msg string, args ...interface{}) {
fmt.Println(args...)
})
// Configuration options tell websocketd where to look for programs to
// run as WebSockets.
config := &wsd.Config{
ScriptDir: "./ws-bin",
UsingScriptDir: true,
StartupTime: time.Now(),
DevConsole: true,
}
// Register your route and handler.
os.ClearEnv();
http.HandleFunc("/ws-bin/", func(rw http.ResponseWriter, req *http.Request) {
handler := http.StripPrefix("/ws-bin", wsd.NewWebsocketdServer(config, logScope, MAXFORKS))
handler.ServeHTTP(rw, req)
})
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
fmt.Println("could not start server!", err)
os.Exit(1)
}
}
Make sure you run go get
to fetch the dependency.
The websocketd user guide is a publicly editable wiki. Please contribute!
Getting Started
Reference
Language specific
Advanced: Internals
Spanish Websocket Wiki
Primeros pasos
Referencia
Lenguajes
Avanzado