y***@163.com
y***@163.com
  • 发布:2023-02-28 10:01
  • 更新:2023-03-01 12:09
  • 阅读:215

websocket接收不到服务器返回的消息

分类:uni-app

客户端发送消息至服务器,服务器返回相应的值到客户端
经过测试,使用hbuilderx内置浏览器和电脑端浏览器,可以正常返回数据
但是使用真机调试、模拟器,打包后都无法接收到

<template>  
    <view>  

    </view>  
</template>  

<script>  
    var _self;  
    var socket = null;  
    export default {  
        data() {  
            return {  

            }  
        },  
        onLoad() {  
            _self = this;  
            _self.connect();  
        },  
        methods: {  
            connect: function() {  
                socket = uni.connectSocket({  
                    url: "wss://warningchain.sejianghu.com:10001",  
                    complete: (res) => {  
                        console.info(res);  
                    }  
                });  
                socket.onOpen(() => {  
                    var data={  
                        flag: "test1",  
                        code:  "test2",  
                        voice:  "test3",  
                        type:  "test4",  
                        msg:  "online"  
                    }   
                    console.info(data);  
                    socket.send({  
                        data: JSON.stringify(data)  
                    });   
                });  
                socket.onClose(function() {  
                    _self.connect();  
                });  
                socket.onMessage((res) => {  
                    console.info(res);  
                });  
                uni.onSocketMessage((res) => {  
                    console.info(res);  
                });  
            }  
        }  
    }  
</script>  

<style>  

</style>  
2023-02-28 10:01 负责人:无 分享
已邀请:
y***@163.com

y***@163.com (作者)

浏览器端是可以接收到的

y***@163.com

y***@163.com (作者)

使用websocket测试工具也是没有问题的

y***@163.com

y***@163.com (作者)

经测试,使用plus-websocket插件,可以解决

note744917

note744917 - 我的联系方式:wx: note744917

uni 的 socket api 没问题,你排除下你代码的原因吧,毕竟我们开发的iOS 和 Android 都正常,千人群聊稳定运行两个月了,没发现你说的这类问题

要回复问题请先登录注册