tgo*_*gos 6 go websocket socket.io
从一个工作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)
Run Code Online (Sandbox Code Playgroud)
前端:
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);
Run Code Online (Sandbox Code Playgroud)
开发人员工具:我注意到一些请求,直到浏览器和服务器选择使用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
Run Code Online (Sandbox Code Playgroud)
服务器日志:
"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
Run Code Online (Sandbox Code Playgroud)
代码来自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)
}
Run Code Online (Sandbox Code Playgroud)
转码输出:
Connected
Run Code Online (Sandbox Code Playgroud)
服务器日志:
"GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 200 0 1.004291
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?使用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
Run Code Online (Sandbox Code Playgroud)
服务器:
(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
Run Code Online (Sandbox Code Playgroud)
请注意,服务器正常运行on.('connect'...)并且on.('join',...)未生成日志.
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')
Run Code Online (Sandbox Code Playgroud)
客户日志:
python3 test.py
[Connected]
[Disconnected]
[Disconnected]
Run Code Online (Sandbox Code Playgroud)
服务器日志:
(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
Run Code Online (Sandbox Code Playgroud)
我认为你做得对,只是你忘记了实际发送消息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")
}
Run Code Online (Sandbox Code Playgroud)
请注意 goroutine 调用的函数,该函数实际上将“加入”消息传递给服务器。
另外,请注意使用 async.WaitGroup来阻塞直到 goroutine 完成,而不是使用time.Sleep()to 等待。
| 归档时间: |
|
| 查看次数: |
4337 次 |
| 最近记录: |