retrocode
retrocode
  • 发布:2019-11-21 21:30
  • 更新:2020-04-26 17:36
  • 阅读:2025

关于uni.connectSocket对接socket.io的问题

分类:uni-app


使用uni.connectSocket无法连接到后台的socket.io,前后台代码检测都没用问题,有人知道原因吗?

后端代码:

var io = null;  
exports.start = function(config, mgr) {  
    io = require('socket.io')(config.CLIENT_PORT);  
    io.sockets.on('connection', function(socket) {  
        console.log('用户已连接', new Date().getTime());  
        socket.on('ping', function(data) {  
            console.log('ping pong!');  
            socket.emit('pong!!');  
        });  
    });  
    console.log("socket.io 初始化成功! listening on " + config.CLIENT_PORT);  
};
    uni.connectSocket({  
      url: 'wss://192.168.0.103:23456',  
      success: res => console.log('WebSocket调用成功!', res),  
      fail: res => console.log('WebSocket连接失败!', res)  
    });  
    uni.onSocketOpen(() => console.log('WebSocket连接已打开!'));  
    uni.onSocketError(() => console.log('WebSocket连接打开失败,请检查!'));  
    uni.onSocketMessage(res => console.log('收到服务器内容:' + res.data));  
    uni.onSocketClose(() => console.log('WebSockPet 已关闭!'));  
    uni.sendSocketMessage({ data: 'ping', complete: res => console.log(res, 'ping') });

2019-11-21 21:30 负责人:无 分享
已邀请:
retrocode

retrocode (作者) - 现已加入旺旺大礼包全家桶

有人吗

  • 3***@qq.com

    解决了吗

    2020-01-06 13:28

  • retrocode (作者)

    回复 3***@qq.com: 解决了,用插件市场中的weapp.socket.io对接就好

    2020-01-07 15:24

7***@qq.com

7***@qq.com - 12112

你这个是包含了app端的吗 app端怎么连接?

  • 7***@qq.com

    测试了一下,可以在app中使用

    2020-02-04 12:16

s***@flashingtime.cn

s***@flashingtime.cn

app端 会报
eportJSException >>>> exception function:createInstanceContext, exception:white screen cause create instanceContext failed,check js stack ->Exception: ReferenceError: Can't find variable: window

该问题目前已经被锁定, 无法添加新回复