我使用websocket可以连接上我们的后台,但是不能发送数据和接收后台传的数据,一直在接收type:ping,这个问题哪位大佬可以帮忙解决一下




export default { data() { return { websock: null, } }, methods: { threadPoxi(){ // 实际调用的方法 //参数 const agentData = "mymessage"; //若是ws开启状态 if (this.websock.readyState === this.websock.OPEN) { this.websocketsend(agentData) } // 若是 正在开启状态,则等待300毫秒 else if (this.websock.readyState === this.websock.CONNECTING) { let that = this;//保存当前对象this setTimeout(function () { that.websocketsend(agentData) }, 300); } // 若未开启 ,则等待500毫秒 else { this.initWebSocket(); let that = this;//保存当前对象this setTimeout(function () { that.websocketsend(agentData) }, 500); } }, initWebSocket(){ //初始化weosocket //ws地址 const wsuri = "ws://192.168.0.115:8102"; this.websock = new WebSocket(wsuri); this.websock.onmessage = this.websocketonmessage; this.websock.onclose = this.websocketclose; }, websocketonmessage(e){ //数据接收 const redata = JSON.parse(e.data); console.log(redata.value); }, websocketsend(agentData){//数据发送 this.websock.send(agentData); }, websocketclose(e){ //关闭 console.log("connection closed (" + e.code + ")"); } }, created(){ this.initWebSocket() } }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用WebSocket在C++中发送接收JSON数据,您需要使用一个WebSocket客户端库,例如Boost.Beast或uWebSockets。在这里,我将为您提供一个使用Boost.Beast的示例代码。 首先,您需要安装Boost库,并在代码中包含以下头文件: ```cpp #include <boost/beast/core.hpp> #include <boost/beast/websocket.hpp> #include <boost/asio/strand.hpp> #include <boost/property_tree/json_parser.hpp> ``` 接下来,您需要定义一个WebSocket客户端对象和一个Boost.Asio io_context对象: ```cpp namespace beast = boost::beast; namespace http = beast::http; namespace websocket = beast::websocket; using tcp = boost::asio::ip::tcp; boost::asio::io_context io_context; websocket::stream<tcp::socket> websocket_stream(io_context); ``` 然后,您需要连接WebSocket服务器: ```cpp tcp::resolver resolver(io_context); auto const results = resolver.resolve("example.com", "80"); boost::asio::connect(websocket_stream.next_layer(), results.begin(), results.end()); websocket_stream.handshake("example.com", "/"); ``` 现在,您已经连接WebSocket服务器。接下来,您可以使用以下代码发送JSON数据: ```cpp boost::property_tree::ptree root; root.put("key1", "value1"); root.put("key2", "value2"); std::ostringstream oss; boost::property_tree::write_json(oss, root, false); websocket_stream.write(boost::asio::buffer(oss.str())); ``` 要接收JSON数据,请使用以下代码: ```cpp beast::multi_buffer buffer; websocket_stream.read(buffer); std::string json_str = boost::beast::buffers_to_string(buffer.data()); std::istringstream iss(json_str); boost::property_tree::ptree root; boost::property_tree::read_json(iss, root); std::string value1 = root.get<std::string>("key1"); std::string value2 = root.get<std::string>("key2"); ``` 这样,您就可以使用WebSocket在C++中发送接收JSON数据了。请注意,此示例代码仅供参考,您需要根据您的具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值