我使用以下lib:https://github.com/gorilla/websocket 如何设置协议和来源,
代码:
func InitWSSClient(url1, protocol, origin string) {
u := url.URL{Scheme: "ws", Host: url1}
fmt.Println(url1,u.String())
d :=websocket.DefaultDialer
//c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
c, _, err :=d.Dial(u.String(),nil)
var limit int64 =1024 * 1024 *32
if err != nil {
log.Fatal("dial:", err)
}
c.SetReadLimit(limit)
go readWSMessage(c)
go sendWSmessage(c)
}
func readWSMessage(ws *websocket.Conn) error {
for {
_,message,err := ws.ReadMessage()
if err != nil {
log15.Error("receiveWebsocket ", "error", err)
panic("error")
}
assignmentWebsocket(string(message))
}
}
func sendWSmessage(ws *websocket.Conn) error {
for {
select {
case params := <-WebsocketParams:
fmt.Println("ws send messages", params)
err :=ws.WriteMessage(websocket.TextMessage,[]byte(params))
if err != nil {
log15.Error("receiveWebsocket ", "error", err)
panic("error")
}
}
}
}
x.net.websocket像这样设置这些元素:
websocket.Dial(url, protocol, origin)
如何在大猩猩/ websocket /中实现此功能,这对您有帮助。 我尝试许多解决方案无法正常工作。如果您知道任何解决方案,
找到解决方案:
d := websocket.DefaultDialer
d.Subprotocols = []string{protocol}
header :=http.Header{}
header.Set("origin",origin)
答案 0 :(得分:1)
我找到了解决方案,您可以这样设置
u := url.URL{Scheme: "ws", Host: url1}
d := websocket.DefaultDialer
d.Subprotocols = []string{protocol}
header := http.Header{}
header.Set("origin", origin)
c, _, err := d.Dial(u.String(), header)