本文整理匯總了Golang中websocket.Handler函數的典型用法代碼示例。如果您正苦於以下問題:Golang Handler函數的具體用法?Golang Handler怎麽用?Golang Handler使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Handler函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
http.Handle("/lobby", websocket.Handler(LobbyServer))
http.Handle("/", http.FileServer(http.Dir("/tmp")))
fmt.Println("Listening on:", listenAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
panic("ListenAndServe: " + err.String())
}
}
示例2: ExampleHandler
// This example demonstrates a trivial echo server.
func ExampleHandler() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
示例3: main
func main() {
flag.Parse()
go hub()
var err error
session, err = mgo.Mongo("localhost")
if err != nil {
panic("main > Mongo: " + err.Error())
}
defer session.Close()
pfx := "/static/"
h := http.StripPrefix(pfx, http.FileServer(http.Dir("../static/")))
http.Handle(pfx, h) // It is absurd I had to work that hard to serve static files. Let's shove them on AWS or something and forget about it
http.HandleFunc("/tickle", doTickle)
http.HandleFunc("/keys", viewKeys)
http.HandleFunc("/keys/add", addKey)
http.HandleFunc("/keys/check", checkKey)
http.HandleFunc("/links/send", sendLink)
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/openid/callback", openID)
http.HandleFunc("/openid/callback/chrome", openID)
http.HandleFunc("/users/validate", validateUser)
http.HandleFunc("/logout", doLogout)
http.HandleFunc("/login", doLogin)
http.HandleFunc("/links/", linksList)
http.HandleFunc("/", rootHandler)
http.Handle("/ws", websocket.Handler(wsHandler))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
示例4: accept
// Accepts a http connection & request pair. It upgrades the connection and calls
// proceed if succesfull.
//
// TODO: Remove the ugly channels and timeouts. They should not be needed!
func (s *websocketSocket) accept(w http.ResponseWriter, req *http.Request, proceed func()) (err os.Error) {
if s.connected {
return ErrConnected
}
f := func(ws *websocket.Conn) {
err = nil
ws.SetReadTimeout(s.t.rtimeout)
ws.SetWriteTimeout(s.t.wtimeout)
s.connected = true
s.ws = ws
s.close = make(chan byte)
defer close(s.close)
proceed()
// must block until closed
<-s.close
}
err = errWebsocketHandshake
if _, ok := req.Header["Sec-Websocket-Key1"]; ok {
websocket.Handler(f).ServeHTTP(w, req)
} else {
websocket.Draft75Handler(f).ServeHTTP(w, req)
}
return
}
示例5: HandleChans
// We export a single function, which creates a page controlled by a
// single websocket. It's quite primitive, and yet quite easy to use!
func HandleChans(url string, handler func(evts <-chan string, pages chan<- string, done <-chan os.Error)) {
myh := func(ws *websocket.Conn) {
evts := make(chan string)
pages := make(chan string)
done := make(chan os.Error)
go handler(evts, pages, done)
go func() {
r := bufio.NewReader(ws)
for {
x, err := r.ReadString('\n')
if err == nil {
evts <- x[:len(x)-1]
} else {
done <- os.NewError("Error from r.ReadString: " + err.String())
return
}
}
}()
for {
x := <-pages
_, err := fmt.Fprintln(ws, x)
if err != nil {
done <- os.NewError("Error in fmt.Fprintln: " + err.String())
return
}
}
}
http.Handle(path.Join(url, "socket"), websocket.Handler(myh))
skeleton := func(c http.ResponseWriter, req *http.Request) {
c.SetHeader("Content-Type", "text/html")
fmt.Fprintln(c, skeletonpage(req))
}
http.HandleFunc(url, skeleton)
}
示例6: main
func main() {
http.Handle("/echo", websocket.Handler(EchoServer))
err := http.ListenAndServe(":12345", nil)
if err != nil {
panic("ListenAndServe: " + err.String())
}
}
示例7: Listen
func (this *WebSocketListener) Listen() {
http.Serve(this.listener, websocket.Handler(func(connection *websocket.Conn) {
// The HTTP package creates the goroutine itself. No need for us to do it.
// Set the RemoteAddr here because of Go Bug #1636
this.ConnectionHandler(this.ircd, connection, connection.Request.RemoteAddr)
}))
}
示例8: evServer
func evServer(w http.ResponseWriter, r *http.Request) {
wevs := make(chan store.Event)
path := r.URL.Path[len("/$events"):]
glob, err := store.CompileGlob(path + "**")
if err != nil {
w.WriteHeader(400)
return
}
rev, _ := Store.Snap()
go func() {
walk(path, Store, wevs)
for {
ch, err := Store.Wait(glob, rev+1)
if err != nil {
break
}
ev, ok := <-ch
if !ok {
break
}
wevs <- ev
rev = ev.Rev
}
close(wevs)
}()
websocket.Handler(func(ws *websocket.Conn) {
send(ws, path, wevs)
ws.Close()
}).ServeHTTP(w, r)
}
示例9: HandleSeparate
// We export a single function, which creates a page controlled by a
// single websocket. It's quite primitive, and yet quite easy to use!
func HandleSeparate(url string, hh func() Handler) {
myh := func(ws *websocket.Conn) {
h := hh()
h.AddSend(func(p string) { fmt.Fprintln(ws, p) })
fmt.Fprintln(ws, "start")
r := bufio.NewReader(ws)
for {
x, err := r.ReadString('\n')
if err == nil {
h.Handle(x[:len(x)-1])
} else {
h.Done(os.NewError("Error from r.ReadString: " + err.String()))
return
}
}
}
http.Handle(path.Join(url, "socket"), websocket.Handler(myh))
skeleton := func(c http.ResponseWriter, req *http.Request) {
c.Header().Set("Content-Type", "text/html")
fmt.Fprintln(c, skeletonpage(req))
}
http.HandleFunc(url, skeleton)
}
示例10: Start
func Start(ch *chan int, st *State.State) {
world = *ch
state = st
go http.ListenAndServe(":25560", http.HandlerFunc(httpServe))
go http.ListenAndServe(":25561", websocket.Handler(wssServe))
// os.ForkExec("http://localhost:25560/index.oc", []string{}, []string{}, "", []*os.File{})
}
示例11: makeWsConnHandler
func makeWsConnHandler(core *WsHttpCore) http.Handler {
return websocket.Handler(func(c *websocket.Conn) {
id := core.RegisterConn(c)
log.Println("send connid", id)
c.Write([]byte(id))
// chanWriter := core.MakeChanWriter(id)
jsChan := core.MakeJsonChan(id)
for {
var data WsHttpMsg
err := websocket.JSON.Receive(c, &data)
if err != nil {
log.Println("Error reading, socket closed")
break
}
log.Println("recvd.", id, data)
jsChan <- &data
}
// _,err := io.Copy(chanWriter,c)
// if err != nil {
// log.Println("wshttp.conn.error",id,err)
// }
// chanWriter.Close()
core.RemoveConn(id)
})
}
示例12: main
func main() {
flag.Parse()
go hub()
http.Handle("/broadcast", websocket.Handler(clientHandler))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Exit("ListenAndServe:", err)
}
}
示例13: main
func main() {
http.Handle("/", websocket.Handler(Echo))
http.Handle("/send", http.HandlerFunc(myHandler))
if err := http.ListenAndServe("localhost:1234", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
示例14: webSocketProtocolSwitch
func webSocketProtocolSwitch(c http.ResponseWriter, req *http.Request) {
// Handle old and new versions of protocol.
if _, found := req.Header["Sec-Websocket-Key1"]; found {
websocket.Handler(clientHandler).ServeHTTP(c, req)
} else {
websocket.Draft75Handler(clientHandler).ServeHTTP(c, req)
}
}
示例15: main
func main() {
http.HandleFunc("/", NotFoundServer)
http.HandleFunc("/test", HtmlResponseServer)
http.Handle("/ws", websocket.Handler(WebSocketServer))
err := http.ListenAndServe(":8888", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}