gpt4 book ai didi

go - 使用 Go 的 socket.io 客户端

转载 作者:IT王子 更新时间:2023-10-29 01:21:08 27 4
gpt4 key购买 nike

从一个有效的 socket.io 示例开始(后端:Python/Flask,前端:socket.io.js v2.0.3),我现在尝试设置一个客户端走却连握手阶段都过不了。很抱歉发了这么长的帖子……(最后我还添加了一个 Python 客户端,它可以完成我想在 Go 中实现的功能)

(以下工作):


后端:

@socketio.on('connect', namespace='/endpoint')
def connect():
print("Client connected with request sid "+request.sid)

@socketio.on('join', namespace='/endpoint')
def join(message):
print("Server received from client:" +message)
print("Client just joined room with request sid "+request.sid)
join_room(request.sid)

前端:

namespace = '/endpoint';
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace);

var client_join_message = "This is a client";
socket.emit('join', client_join_message);

开发人员工具:在浏览器和服务器选择使用 websockets 和交换框架之前,我注意到一些请求:

http://localhost:5000/socket.io/?EIO=3&transport=polling&t=LxcgetJ
http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Lxcgetf&sid=025e105a5093467d994a891367380aa3
http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Lxcgeti&sid=025e105a5093467d994a891367380aa3
ws://localhost:5000/socket.io/?EIO=3&transport=websocket&sid=025e105a5093467d994a891367380aa3
http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Lxcgetw&sid=025e105a5093467d994a891367380aa3
http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Lxcgetx&sid=025e105a5093467d994a891367380aa3

服务器日志:

"GET /socket.io/?EIO=3&transport=polling&t=LxcgetJ HTTP/1.1" 200 381 0.000322
Client connected with request sid 025e105a5093467d994a891367380aa3
"POST /socket.io/?EIO=3&transport=polling&t=Lxcgetf&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 219 0.000806
(6450) accepted ('127.0.0.1', 45034)
"GET /socket.io/?EIO=3&transport=polling&t=Lxcgeti&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 227 0.003941
"POST /socket.io/?EIO=3&transport=polling&t=Lxcgetw&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 219 0.001650
"GET /socket.io/?EIO=3&transport=polling&t=Lxcgetx&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 215 0.000235
Server received from client:This is a client
Client just joined room with request sid 025e105a5093467d994a891367380aa3


我对 Go 的尝试,(不起作用):


代码来自这个client github.com/graarh/golang-socketio 中的示例:

package main

import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"log"
"runtime"
"time"
)

func main() {

runtime.GOMAXPROCS(runtime.NumCPU())

c, err := gosocketio.Dial(
gosocketio.GetUrl("127.0.0.1", 5000, false),
transport.GetDefaultWebsocketTransport())
if err != nil {
log.Fatal(err)
}

err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
log.Fatal("Disconnected")
})
if err != nil {
log.Fatal(err)
}

err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
log.Println("Connected")
})
if err != nil {
log.Fatal(err)
}

time.Sleep(1 * time.Second)
}

Go代码输出:

Connected

服务器日志:

"GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 200 0 1.004291

有什么我想念的吗?使用 Go 代码,我看不到任何 sidtransport=polling...此外,只有少数问题标记为 [go] [socket .io],这让我觉得我选择了错误的道路......如果有任何想法,我将不胜感激。


@约翰韦尔登

您的代码生成以下内容:

客户:

$ go run gotest5.go 
2017/10/11 11:21:40 Connected
2017/10/11 11:21:40 result ""
2017/10/11 11:21:40 Done

服务器:

(4380) wsgi starting up on http://127.0.0.1:5000
(4380) accepted ('127.0.0.1', 38860)
127.0.0.1 - - [11/Oct/2017 11:21:40] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 200 0 0.003100

请注意,服务器函数 on.('connect'...)on.('join',...) 没有生成日志。


Python 客户端(有效):

from socketIO_client import SocketIO, BaseNamespace

class ThisNamespace(BaseNamespace):
def on_connect(self):
print('[Connected]')
def on_reconnect(self):
print('[Reconnected]')
def on_disconnect(self):
print('[Disconnected]')

with SocketIO('127.0.0.1', 5000, ThisNamespace) as socketIO:
this_namespace = socketIO.define(ThisNamespace, '/endpoint')

客户端日志:

python3 test.py 
[Connected]
[Disconnected]
[Disconnected]

服务器日志:

(6047) wsgi starting up on http://127.0.0.1:5000
(6047) accepted ('127.0.0.1', 38900)
127.0.0.1 - - [11/Oct/2017 11:53:27] "GET /socket.io/?t=1507712007314-0&transport=polling&EIO=3 HTTP/1.1" 200 381 0.000859
(6047) accepted ('127.0.0.1', 38902)
Client connected with request sid 919ed69264dd4e9f93e7af0294970dbd
Client disconnected with request.sid 919ed69264dd4e9f93e7af0294970dbd
127.0.0.1 - - [11/Oct/2017 11:53:27] "GET /socket.io/?sid=919ed69264dd4e9f93e7af0294970dbd&transport=websocket&EIO=3 HTTP/1.1" 200 0 0.032171

最佳答案

我认为你做得对,只是你忘了实际发送 join 消息,可能是这样的:

package main

import (
"log"
"runtime"
"sync"
"time"

"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
)

func doSomethingWith(c *gosocketio.Client, wg *sync.WaitGroup) {
if res, err := c.Ack("join", "This is a client", time.Second*3); err != nil {
log.Printf("error: %v", err)
} else {
log.Printf("result %q", res)
}
wg.Done()
}

func main() {

runtime.GOMAXPROCS(runtime.NumCPU())

c, err := gosocketio.Dial(
gosocketio.GetUrl("127.0.0.1", 3003, false),
transport.GetDefaultWebsocketTransport())
if err != nil {
log.Fatal(err)
}

err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
log.Fatal("Disconnected")
})
if err != nil {
log.Fatal(err)
}

err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
log.Println("Connected")
})
if err != nil {
log.Fatal(err)
}

wg := &sync.WaitGroup{}
wg.Add(1)
go doSomethingWith(c, wg)
wg.Wait()
log.Printf("Done")
}

请注意 goroutine 对实际将“加入”消息传递给服务器的函数的调用。

另外,请注意使用 sync.WaitGroup 来阻塞直到 goroutine 完成,而不是使用 time.Sleep() 来等待。

关于go - 使用 Go 的 socket.io 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46561747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com