【发布时间】:2017-10-04 09:43:56
【问题描述】:
从一个有效的socket.io 示例开始(后端:Python/Flask,前端:socket.io.js v2.0.3),我现在尝试使用 Go 设置客户端,但甚至无法通过握手阶段。为长篇道歉......(最后我还添加了一个 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 的尝试,(不起作用):
代码来自github.com/graarh/golang-socketio中的这个client示例:
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 代码,我看不到任何 sid、transport=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
【问题讨论】:
-
Dial函数用于作为客户端连接到服务器,但您正在尝试创建服务器。您需要使用库文档中显示的NewServer。 -
谢谢加文。我想连接到服务器,而不是创建一个新的。所以
Dial()的使用在这里是正确的,不是吗? -
抱歉,我误读并认为您试图将后端移动到 Go。我注意到您当前的设置,通过 http 的请求被记录并具有
transport=polling,通过 ws 的请求被记录为客户端连接和transport=websocket。但是,当您使用 Go 客户端时,您会使用transport=websocket记录一个 http 请求。 -
是的,你是对的。通过深入挖掘golang-socketio 的源代码,我看不到任何选项可以在浏览器中执行与 javascript 相同的操作。这个握手/轮询阶段也没有很好的记录:Document the handshake process #10
-
事实上,你的 JS 和 Go 客户端的工作方式不同。 JS 使用轮询,Go 尝试使用 websockets。您能否仅将可用传输限制为 websocket?像这样:
io.connect("http://your.domain.com", {transports: ['websocket']});