當前位置: 首頁>>代碼示例>>Golang>>正文


Golang websocket.Handler函數代碼示例

本文整理匯總了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())
	}
}
開發者ID:uglybrd,項目名稱:GoGameHTML5,代碼行數:9,代碼來源:server.go

示例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())
	}
}
開發者ID:npe9,項目名稱:minimega,代碼行數:8,代碼來源:examplehandler_test.go

示例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)
	}
}
開發者ID:klinster,項目名稱:Bessie,代碼行數:31,代碼來源:twocloud.go

示例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
}
開發者ID:andradeandrey,項目名稱:go-socket.io,代碼行數:33,代碼來源:transport_websocket.go

示例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)
}
開發者ID:droundy,項目名稱:abridge,代碼行數:37,代碼來源:interactive.go

示例6: main

func main() {
	http.Handle("/echo", websocket.Handler(EchoServer))
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		panic("ListenAndServe: " + err.String())
	}
}
開發者ID:taysom,項目名稱:tau,代碼行數:7,代碼來源:socket.go

示例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)
	}))
}
開發者ID:ahf,項目名稱:ircd-novo,代碼行數:7,代碼來源:listeners.go

示例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)
}
開發者ID:Br3nda,項目名稱:doozerd,代碼行數:34,代碼來源:web.go

示例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)

}
開發者ID:mattn,項目名稱:gui,代碼行數:27,代碼來源:websocket.go

示例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{})
}
開發者ID:CasualSuperman,項目名稱:Ourcraft,代碼行數:7,代碼來源:gui.go

示例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)
	})
}
開發者ID:cheng81,項目名稱:goexp-wshttp,代碼行數:26,代碼來源:wshttp.go

示例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)
	}
}
開發者ID:blindsey,項目名稱:wsbench,代碼行數:8,代碼來源:broadcast_server.go

示例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)
	}
}
開發者ID:likesea,項目名稱:GoLearn,代碼行數:8,代碼來源:webSocketServer.go

示例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)
	}
}
開發者ID:ngd,項目名稱:chrome-bench,代碼行數:8,代碼來源:flicker.go

示例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)
	}
}
開發者ID:Saikees,項目名稱:practice,代碼行數:9,代碼來源:websocket.go


注:本文中的websocket.Handler函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。