如何将事件更新从http处理程序发送到WebSocket处理程序

时间:2019-03-11 01:31:15

标签: go websocket

我是Go语言的新手,我想将自己的想法缠绕在各个频道上。为了清楚我的理解,我看了视频教程,读了一些书,但是当我进行实际编码和使用Go编码的Web应用程序中的频道使用时,仍然感到困惑。

我想要做的是拥有2个URL:

  1. 通常的常规GET或POST URL,显示或获取值,以及 处理它。在后端进行一些处理,我希望 处理要在websocket更新中发送到相同输出的输出 URL,因此不需要刷新/重新加载窗口。
  2. 基于Gorilla软件包的websocket URL。

以下是我到目前为止尝试过的测试代码,它仍然是我为寻求解决方案而编写的混乱代码的简化版本:

//file main.go
package main

import (
    "io"
    "net/http"
    "fmt"
    "math/rand"
    "log"
    "time"
    "github.com/gorilla/websocket"
)


func logging(f http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        log.Println(r.URL.Path)
        if r.URL.Path == `/ws` {
            log.Println("WebSocket is accessed from ws://localhost:8080/ws")
        }
        f(w, r)
    }
}

type hotcat int

func (c hotcat) ServeHTTP(res http.ResponseWriter, req *http.Request) {
    io.WriteString(res, "cat cat cat")

    //Some code here who's output I want to pass to websockets url ws://localhost:8080/ws
    n := timeConsumingWork(4)
    fmt.Println("Random Number Print from cat: ", n)
    //Example the value of n I need to pass to ws://localhost:8080/ws, how can I do it?

    // Some other example test code just giving some random output from hotcat http handler
    // Would like to pass it's output to ws://localhost:8080/ws to print in websocckets output in browser
    go func(){
        out := make(chan string)
        go func(){
            for i := 0; ; i++ {
                out <- `foo said something`
                time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
            }
            //out <- `foo said something`
        }()
        printer(out)
    }()
}

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
    CheckOrigin: func(r *http.Request) bool {
        return true
    },
}

// Execute this in browser console to initiate websococket connection and to send ws.send() commands etc.
/*
var ws = new WebSocket("ws://localhost:8080/ws")
ws.addEventListener("message", function(e) {console.log(e);});
ws.onmessage = function (event) {
    console.log(event.data);
}

ws.send("foo")
ws.send(JSON.stringify({username: "Sat"}))

ws.readyState
ws.CLOSED
ws.OPEN
ws.close()
*/
func ws(w http.ResponseWriter, r *http.Request) {
    socket, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        fmt.Println(err)
        return
    }
    for {
        msgType, msg, err := socket.ReadMessage()
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(string(msg))
        if err = socket.WriteMessage(msgType, msg); err != nil {
            fmt.Println(err)
        }
    }
}

func main() {
    var c hotcat

    http.Handle("/cat", c)
    http.HandleFunc("/ws", logging(ws))

    http.ListenAndServe(":8080", nil)
}


func timeConsumingWork(n int) int {
    time.Sleep(time.Microsecond * time.Duration(rand.Intn(500)))
    return n + rand.Intn(1000)
}


func printer(in <-chan string) {
    //log.Println(<-in)
    go func() {
        for {
            log.Println(<-in)
        }
    }()
}
# command shell output
$ go run main.go 

Random Number Print from cat:  891
2019/03/11 14:15:32 foo said something
2019/03/11 14:15:33 /ws
2019/03/11 14:15:33 WebSocket is accessed from ws://localhost:8080/ws
foo
2019/03/11 14:15:34 foo said something
2019/03/11 14:15:34 foo said something
2019/03/11 14:15:34 foo said something
2019/03/11 14:15:36 foo said something
2019/03/11 14:15:36 foo said something
^Csignal: interrupt
$ 

websocket command input from browser

我想在浏览器的websocket输出中显示随机输出字符串“ 2019/03/11 14:15:34 foo said something”。

我非常感谢您的指导或帮助。

我认为该问题的代码,终端输出和浏览器屏幕截图中的注释应可以清楚我正在尝试执行的操作,但是如果此问题仍不清楚,请告诉我,我将尝试在其上进行扩展更多。

感谢与问候,

Satinder

更新1:

我阅读并尝试了聊天应用程序的Mat Ryer示例:https://github.com/matryer/goblueprints/tree/master/chapter1/chat

这里可能是代码副本:https://github.com/satindergrewal/golang-practice/tree/master/chat-examples/mychat02

从示例中可以理解,如果我具有Web套接字句柄,则可以将消息从该Websocket http句柄传递到其他已连接的Web客户端。但是我仍然很困惑如何将消息从非websocket的http句柄发送到websocket的句柄路由/地址。

我知道我不能只将这段代码用于/的{​​{1}}:

ServeHTTP

它已经给我以下错误:

// ServeHTTP handles the HTTP request.
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)))
    })
    fmt.Println(r.Host)
    t.templ.Execute(w, r)
    var room *room
    socket, _ := upgrader.Upgrade(w, r, nil)
    client := &client{
        socket: socket,
        send:   make(chan []byte, messageBufferSize),
        room:   room,
    }
    room.join <- client
    go func() {
        for i := 0; i < 10; i++ {
            time.Sleep(time.Duration(rand.Intn(8e3)) * time.Millisecond)
            client.socket.WriteMessage(websocket.TextMessage, []byte("Hello from / ServeHTTP Handle"))
            //fmt.Println("Sending automatic hello from root ServeHTTP handle to web page!")
        }
    }()
}

更新2::再次尝试重新阅读评论的第一条答复后,尝试过不同的做法。

localhost:8080
2019/03/25 11:19:18 http: superfluous response.WriteHeader call from github.com/gorilla/websocket.(*Upgrader).returnError (server.go:81)
2019/03/25 11:19:18 http: panic serving [::1]:52691: runtime error: invalid memory address or nil pointer dereference
goroutine 39 [running]:
net/http.(*conn).serve.func1(0xc00013e140)
        /usr/local/Cellar/go/1.12.1/libexec/src/net/http/server.go:1769 +0x139
panic(0x13a3e00, 0x172dc20)
        /usr/local/Cellar/go/1.12.1/libexec/src/runtime/panic.go:522 +0x1b5
main.(*templateHandler).ServeHTTP(0xc00008ef60, 0x14954a0, 0xc0001dc380, 0xc000214200)
        /Users/satinder/go/src/golang-practice/chat-examples/mychat02/main.go:40 +0x209
net/http.(*ServeMux).ServeHTTP(0x173cfa0, 0x14954a0, 0xc0001dc380, 0xc000214200)
        /usr/local/Cellar/go/1.12.1/libexec/src/net/http/server.go:2375 +0x1d6
net/http.serverHandler.ServeHTTP(0xc000130000, 0x14954a0, 0xc0001dc380, 0xc000214200)
        /usr/local/Cellar/go/1.12.1/libexec/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc00013e140, 0x1495ba0, 0xc0000a0380)
        /usr/local/Cellar/go/1.12.1/libexec/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.12.1/libexec/src/net/http/server.go:2884 +0x2f4

现在它不给出错误,但是我看不到控制台显示添加了第二个客户端,我希望通过命令行添加// ServeHTTP handles the HTTP request. 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))) }) fmt.Println(r.Host) t.templ.Execute(w, r) room := newRoom() go func() { for i := 0; i < 10; i++ { time.Sleep(time.Duration(rand.Intn(8e3)) * time.Millisecond) room.forward <- []byte("Hello from / ServeHTTP Handle") //client.socket.WriteMessage(websocket.TextMessage, []byte("Hello from / ServeHTTP Handle")) fmt.Println("Sending automatic hello from root ServeHTTP handle to web page!") } }() }

/

再次尝试使用以前的代码和新的代码进行混合,结果如下:

GoldenBook:mychat02 satinder$ go build -o chat 
GoldenBook:mychat02 satinder$ ./chat 
2019/03/25 11:37:49 Starting web server on :8080
localhost:8080
New client joined
^C
// ServeHTTP handles the HTTP request.
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)))
    })
    fmt.Println(r.Host)
    t.templ.Execute(w, r)

    room := newRoom()
    socket, _ := upgrader.Upgrade(w, r, nil)
    client := &client{
        socket: socket,
        send:   make(chan []byte, messageBufferSize),
        room:   room,
    }
    room.join <- client
    go func() {
        for i := 0; i < 10; i++ {
            time.Sleep(time.Duration(rand.Intn(8e3)) * time.Millisecond)
            room.forward <- []byte("Hello from / ServeHTTP Handle")
            //client.socket.WriteMessage(websocket.TextMessage, []byte("Hello from / ServeHTTP Handle"))
            //fmt.Println("Sending automatic hello from root ServeHTTP handle to web page!")
        }
    }()
}

仍然感到困惑...

有人可以给我解决方法吗?非常感谢您的帮助。

谢谢。

Satinder

1 个答案:

答案 0 :(得分:0)

认为我解决了。至少,即使它可能不是正确的处理方式,它也可以按照我想像的方式执行我想要的代码。正确的做事方式或有效的做这件事的有效方法,我想你们中的许多人都可以纠正我并帮助我做到这一点,希望如此,因此,如果您认为这是错误的或无效的,请发表评论并纠正我。 >

这是我解决的方法:

我查看了大猩猩的websocket示例的echo示例(https://github.com/gorilla/websocket/blob/master/examples/echo/client.go),并从clients.go文件中获取了一些基本代码,该文件将websocket作为一个连接客户。

我的最终目标是将事件更新从另一个http句柄发送到websocket,因此,我正在控制台输出中模拟某些字符串的打印日志示例。

这是我在Mat Ryer聊天示例代码的main.go文件中所做的更改:

// ServeHTTP handles the HTTP request.
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)))
    })
    fmt.Println(r.Host)
    t.templ.Execute(w, r)

    // Creating the URL scheme to use with websocket Dialer
    // to connnect to ws://localhost:8080/room
    u := url.URL{Scheme: "ws", Host: "localhost:8080", Path: "/room"}
    log.Printf("connecting to %s", u.String())

    // Initiate the websocket connection from the go code **as a client** to connect to the chat room
    c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
    if err != nil {
        log.Fatal("dial:", err)
    }

    go func() {
        for i := 0; i < 10; i++ {
            time.Sleep(time.Duration(rand.Intn(8e3)) * time.Millisecond)
            // Just printing the log of the same message in command line. Might better to ignore it.
            // log.Println("Sending automatic hello from root ServeHTTP handle to web page!")

            // Write the Message as Text message to the web socket connection
            // which will show up in the chat box
            err := c.WriteMessage(websocket.TextMessage, []byte("Sending automatic hello from root ServeHTTP handle to web page!"))
            if err != nil {
                log.Println("write:", err)
                return
            }
        }
    }()
}

构建二进制文件后运行此代码将在控制台中输出以下示例输出:

GoldenBook:mychat02 satinder$ ./chat 
2019/03/31 03:44:27 Starting web server on :8080
localhost:8080
2019/03/31 03:44:31 connecting to ws://localhost:8080/room
New client joined
New client joined
2019/03/31 03:44:33 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:36 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:43 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:45 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:45 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
Message received: HELLO
-- sent to client
-- sent to client
2019/03/31 03:44:48 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:48 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:49 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
2019/03/31 03:44:52 Sending automatic hello from root ServeHTTP handle to web page!
Message received: Sending automatic hello from root ServeHTTP handle to web page!
-- sent to client
-- sent to client
^C

以下是显示在网页http://localhost:8080/

上的输出

chat window screenshot

这是完整的代码更新: https://github.com/satindergrewal/golang-practice/tree/master/chat-examples/mychat02

由于这是示例代码,我想为我的实际应用程序(基于WebTTY的应用程序)解决该问题,因此我将能够使用此代码将WebTTY会话结束事件更新从其他http句柄发送到websocket位。

我仍然可以使用在线大师的帮助。

我是否需要在此拨号器附近添加某个频道?我想是的,但是如果有人可以解决其中的任何错误或预期的低效率,我将不胜感激。

非常感谢@cerise-limón到目前为止提供的帮助。 :-)