Giter Site home page Giter Site logo

Comments (15)

andeya avatar andeya commented on May 3, 2024 1

@23233 已经有扩展包实现了 https://github.com/henrylee2cn/teleport/tree/v5/mixer/websocket

from erpc.

andeya avatar andeya commented on May 3, 2024 1

对,body是json的,bodyCodec应该是 'j' ,没错次是 106

from erpc.

andeya avatar andeya commented on May 3, 2024

兼容socket.io的协议,你可以在服务端使用teleport自定义实现相应的socket.Proto接口

from erpc.

AlexStacker avatar AlexStacker commented on May 3, 2024

先谢啦,晚上回去研究一下怎么实现。

from erpc.

23233 avatar 23233 commented on May 3, 2024

@AlexStacker 研究出来了吗?

from erpc.

Natuka avatar Natuka commented on May 3, 2024

请教下,基于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.

andeya avatar andeya commented on May 3, 2024

猜测是前后两次 receiveData.seq 相同

from erpc.

Natuka avatar Natuka commented on May 3, 2024

感谢回复,
receiveData.seq,这边我是根据后端推送过来的数据,然后作为发送数据作为测试用,
我这边把数据修改为相同和不同,得到的效果是相同的,看源码是通过seq去匹配后端的注册路由。

from erpc.

andeya avatar andeya commented on May 3, 2024

seq必须是唯一ID

from erpc.

Natuka avatar Natuka commented on May 3, 2024

好的,我在试试看。

from erpc.

Natuka avatar Natuka commented on May 3, 2024

我这边尝试了下

{
  "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.

andeya avatar andeya commented on May 3, 2024

格式 {"seq":%d,"mtype":%d,"serviceMethod":%q,"meta":%q,"bodyCodec":%d,"body":"%s","xferPipe":%s}

from erpc.

Natuka avatar Natuka commented on May 3, 2024

感谢回复,
这个我之前有看过范例,不过传参时,最早也是采用下划线的形式,但是打印源码之后,发现解析出来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.

Natuka avatar Natuka commented on May 3, 2024

还有就是body_codec 这个值必须固定是106吗?如果不是,我这边要如何传参?

from erpc.

Natuka avatar Natuka commented on May 3, 2024

好的,感谢。

from erpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.