func (r *room) serveHTTP(w http.ResponseWriter, req *http.Request) { socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP: ", err) return } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("Failed to get auth cookie:", err) return } client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, userData: objx.MustFromBase64(authCookie.Value), } r.join <- client defer func() { r.leave <- client }() go client.read() client.write() }
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { socket, err := upgrader.Upgrade(w, req, nil) // Upgrade upgrades the HTTP server connection to the WebSocket protocol. if err != nil { log.Fatal("ServeHTTP:", err) return } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("クッキーの取得に失敗しました:", err) return } client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, userData: objx.MustFromBase64(authCookie.Value), } r.join <- client defer func() { r.leave <- client }() go client.write() client.read() }
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { // need to upgrade socket (HTTP Request) into webSocket socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP:", err) return } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("Failed to get auth cookie:", err) return } // creates a client that will join a channel later on client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, // weird as it is this ',' is correct userData: objx.MustFromBase64(authCookie.Value), } // makes client join (in other words, sends message to 'join' channel) r.join <- client // makes client leave but defer it to ensure clients successfuly join first defer func() { r.leave <- client }() // similar to JS promises go client.write() // goroutine, in other words, run this portion of code in a new thread client.read() }
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { // UpgradeでWebSocketコネクションを取得する socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP:", err) } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("クッキーの取得に失敗しました: ", err) return } client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, userData: objx.MustFromBase64(authCookie.Value), } r.join <- client defer func() { r.leave <- client }() go client.write() // goroutineとして呼び出される client.read() }
func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { // In order to use web sockets, we must upgrade the HTTP // connection using the websocket.Upgrader, which is reusable socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP:", err) return } // get the user data from the auth cookie authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("Failed to get auth cookie:", err) return } client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, // store the user data in client userData: objx.MustFromBase64(authCookie.Value), } r.join <- client defer func() { r.leave <- client }() // run the write method as a go routine // in a different thread go client.write() // call in the main thread which will block // operations(keeping the connection alive) // until it's time to close it client.read() }
// ServeHTTPメソッドを定義することで*roomはhttp.Handlerとして扱えるようになる // HTTPリクエストが発生した場合 func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { // WebSocketコネクションの取得 socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP:", err) return } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("Failed to get auth cookie:", err) return } // クライアントの生成 client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, userData: objx.MustFromBase64(authCookie.Value), } r.join <- client // クライアントの終了時に退室する defer func() { r.leave <- client }() // goroutineとして実行される // 別スレッドで実行される // ※メインスレッドで実行されることもある go client.write() // 接続は保持され、終了を指示されるまで他の処理はブロックされる client.read() }
// means a room can now act as a handler func (r *room) ServeHTTP(w http.ResponseWriter, req *http.Request) { // In order to use web sockets, we must upgrade the HTTP connection using // the websocket.Upgrader type, which is reusable so we need only create one. socket, err := upgrader.Upgrade(w, req, nil) if err != nil { log.Fatal("ServeHTTP: ", err) return } authCookie, err := req.Cookie("auth") if err != nil { log.Fatal("Failed to get auth cookie:", err) return } client := &client{ socket: socket, send: make(chan *message, messageBufferSize), room: r, userData: objx.MustFromBase64(authCookie.Value), } r.join <- client // will call after the operation finished defer func() { r.leave <- client }() // The write method for the client is then called as a Go routine // the word go followed by a space character go client.write() client.read() }
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.once.Do(func() { t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) }) data := map[string]interface{}{ "Host": r.Host, } if authCookie, err := r.Cookie("auth"); err == nil { data["UserData"] = objx.MustFromBase64(authCookie.Value) } t.templ.Execute(w, data) }
func (t *templateHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // no matter how many go routines call it , the function inside do will be executed once only t.once.Do(func() { t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) }) data := map[string]interface{}{ "Host": req.Host, } if authCookie, err := req.Cookie("auth"); err == nil { data["UserData"] = objx.MustFromBase64(authCookie.Value) } t.templ.Execute(rw, data) }
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { t.once.Do(func() { //此处代码只执行一次 多个携程中也只执行一次 t.temp1 = template.Must(template.ParseFiles(filepath.Join("tpl", t.filename))) }) data := map[string]interface{}{ "Host": r.Host, } if authCookie, err := r.Cookie("auth"); err == nil { data["UserData"] = objx.MustFromBase64(authCookie.Value) } if err := t.temp1.Execute(w, data); err != nil { fmt.Println(err) } }
// ServeHTTP handles the HTTP request. func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // the sync.Once type guarantees that the function // we pass as an argument will only be executae once, // regardless of how many goroutines are calling ServeHTTP t.once.Do(func() { t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename))) }) // create a data object to serve the template data := map[string]interface{}{ "Host": r.Host, } // store the auth info from cookie into the data obj if there is any if authCookie, err := r.Cookie("auth"); err == nil { data["UserData"] = objx.MustFromBase64(authCookie.Value) } // t.templ.Execute(w, r) t.templ.Execute(w, data) }