Comments (15)
@23233 已经有扩展包实现了 https://github.com/henrylee2cn/teleport/tree/v5/mixer/websocket
from erpc.
对,body是json的,bodyCodec应该是 'j' ,没错次是 106
from erpc.
兼容socket.io的协议,你可以在服务端使用teleport自定义实现相应的socket.Proto接口
from erpc.
先谢啦,晚上回去研究一下怎么实现。
from erpc.
@AlexStacker 研究出来了吗?
from erpc.
请教下,基于h5 websocket客户端有范例吗?目前基于websocket进行传输数据时,服务端可接收数据但提示
[WARN] not found call cmd:
{
"seq": 112,
"mtype": 2,
"serviceMethod": "/pong/receive",
"status": "",
"meta": "",
"bodyCodec": 0,
"body": null,
"xferPipe": [],
"size": 161
}
客户端代码
var socket = new WebSocket("ws://127.0.0.1:8080/socket.io/");
socket.onopen = function () {
console.log("open");
send();
};
socket.send(`{
"seq": ${receiveData ? receiveData.seq : 0},
"mtype": 2,
"uri": "/pong/receive",
"serviceMethod": "/pong/receive",
"meta": "",
"body_codec": 106,
"body": "5",
"xfer_pipe": []
}`);
from erpc.
猜测是前后两次 receiveData.seq 相同
from erpc.
感谢回复,
receiveData.seq,这边我是根据后端推送过来的数据,然后作为发送数据作为测试用,
我这边把数据修改为相同和不同,得到的效果是相同的,看源码是通过seq去匹配后端的注册路由。
from erpc.
seq必须是唯一ID
from erpc.
好的,我在试试看。
from erpc.
我这边尝试了下
{
"seq": 1,
"mtype": 2,
"uri": "/pong/receive",
"serviceMethod": "/pong/receive",
"meta": "",
"bodyCodec": 106,
"body": "5",
"xfer_pipe": []
}
中的mtype
类型是TypeReply,TypePush,TypeCall,对于客户端来说,貌似调用的是TypeReply,但使用后发现是go 客户端调用的,直接用h5的websocket调用时,提示[WARN] not found call cmd:
,后又尝试了使用TypeCall,这个提示read message: invalid character 'x' after top-level valuesend:
, 现在对h5 websocket发送给服务端和服务端接收处理消息,目前无法正常收到消息,可能是我这边使用方式出现了问题,所以有h5的范例
可供参考下?
还有就是对于mtype:1
需要指定bodyCodec
的值,如果没有设定,那么后端无法接收消息
服务端我这边采用RouteCall
注册路由的。
服务端代码
func CreateWebSocket() *websocket.Server {
wsServer = websocket.New()
wsServer.AddRoute(new(Ping))
wsServer.AddRoute(new(Pong))
wsServer.AddRoute(new(Other))
go _wsPing()
//go _wsClient()
return wsServer
//wsServer.Dial()
//go _wsPing()
}
// 注册路由,框架采用beego
server := socket.CreateWebSocket()
beego.Handler("/socket.io", server.Handler())
// server
func (srv *Server) Handler() http.Handler {
return ws.NewJSONServeHandler(srv.Peer, nil)
}
// 路由
type Pong struct{ erpc.CallCtx }
func (p *Pong) Receive(arg *string) (string, *erpc.Status) {
p.Printf("\nreceive pong: %v", *arg)
return "oooo", erpc.NewStatus(0, "hhh")
}
type Ping struct{ erpc.CallCtx }
func (p *Ping) Receive(arg *string) (string, *erpc.Status) {
p.Printf("\nreceive ping: %v\n", *arg)
return "xxxxxx", erpc.NewStatus(0, "hhh")
}
type Other struct{ erpc.CallCtx }
func (p *Other) Receive(arg *string) (string, *erpc.Status) {
p.Printf("\nreceive Other: %v\n", *arg)
return "xxxxxx", erpc.NewStatus(0, "xxxxxx")
}
from erpc.
格式 {"seq":%d,"mtype":%d,"serviceMethod":%q,"meta":%q,"bodyCodec":%d,"body":"%s","xferPipe":%s}
from erpc.
感谢回复,
这个我之前有看过范例,不过传参时,最早也是采用下划线的形式,但是打印源码之后,发现解析出来body_codec
的值是0,因为json解析时我记得是需要在结构体后面做标记定义。
现在已经通了,不过我不解的是,body里面必须要json字符串?我发现传入的参数为
var sendData = {
seq: receiveData ? receiveData.seq : 0,
mtype: 1,
uri: "/other/receive",
serviceMethod: "/other/receive",
// meta: "",
bodyCodec: 106,
body: "5xxxxx",
xferPipe: [],
};
时,后端接收后提示invalid character 'x' after top-level valuesend:
, 然后我这边去掉之后就正常了。
from erpc.
还有就是body_codec
这个值必须固定是106
吗?如果不是,我这边要如何传参?
from erpc.
好的,感谢。
from erpc.
Related Issues (20)
- quic-go doesn't build on go 1.19 yet HOT 6
- 升级go.mod 和 quic-go 升级不兼容 HOT 3
- 使用govendor后的报错 HOT 2
- TLS handshake error from 10.x.x.x:53786 EOF HOT 1
- How Teleport upgrade to erpc? HOT 1
- c++ client HOT 3
- 您好,请问有详细的使用文档嘛 HOT 1
- 运行提示 undefined: cpu.HasAES错误 HOT 1
- GOARCH=arm 编译失败,存在类型不匹配问题 HOT 3
- 6.3.2版本编译失败 HOT 2
- PostDialPlugin:auth-bearer 无法重连 HOT 2
- 无法安装 HOT 1
- lucas-clemente/quic-go 依赖版本过低 HOT 1
- Socket
- defferences between erpc and go-micro? HOT 2
- 被攻击了...崩溃了.. HOT 2
- router 能不能暴露一个delete 方法 HOT 1
- Flat buffers support HOT 2
- 有没办法服务端对客户端的连接进行限制?
- invalid PreWriteCallPlugin in router: secure(encrypt/decrypt)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from erpc.