收藏
回答

Websocket open一直超时?

客户端代码:

// pages/allChat/index.js
const {
  $Message
} = require('../../dist/base/index');
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    host: '127.0.0.1:8081',
    openid: ''
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        if (res.data) {
          $Message({
            content: '成功进入',
            type: 'success'
          });
          _this.setData({
            openid: res.data
          })
        }
      },
    })
    this.SocketTask = wx.connectSocket({
      url: `wss://${this.data.host}`
    })
  },
  inputEvent: function (e) {
    this.setData({
      content: e.detail.detail.value
    })
  },
  submit: function () {
    try{
      this.SocketTask.onOpen((res) => {
        console.log(res);
      })
    }catch(err){
      console.log(err);
    }
  }
})



node:


var ws = require('nodejs-websocket')
 
var server = ws.createServer((conn)=>{
    console.log('New Connect');
    conn.on('text',(res)=>{
        console.log(res);
        boardcast(res)
    })
    conn.on('error',(err)=>{
        console.log(err);
    })
}).listen(8081,()=>{console.log('监听中...');});
 
 
function boardcast(res){
    server.connections.forEach((conn)=>{
        conn.sendText(res)
    })
}


最后一次编辑于  09-03
回答关注问题邀请回答
收藏
问题标签