Coder Social home page Coder Social logo

Comments (4)

gongzili456 avatar gongzili456 commented on May 22, 2024

请展示您的代码,方便我们排查

from weapp.socket.io.

niefang2017 avatar niefang2017 commented on May 22, 2024
const io = require('../utils/weapp.socket.io.js')
let socket = null
let url = null;
function createConnect(conn = { url: "", room:"socketRoom",response:()=>{},onLine:()=>{},onClose:()=>{},onError:()=>{},onSystem:()=>{}}) {
  url = conn.url
  socket = (this.socket = io(
    url,
    {
      query: {
        room: conn.room,
        userId: `client_${Math.random()}`,
      },
      transports: ['websocket']
    }
  ))
  console.log("socket",socket)
  const id = socket.id
  socket.on('connect', () => {
  })
  socket.on(id, msg => {
    conn.response(msg)
  });
  socket.on('online', (msg) => {
    conn.onLine(msg)
  })
  socket.on('system', (msg) => {
    conn.onSystem(msg)
  })
  socket.on('disconnect', msg => {
    conn.onClose(msg)
  });
  socket.on('error', (e) => {
    conn.onError(e)
  });
}
function closeSocket(){
  if (this.socket) {
    this.socket.close()
    this.socket = null
    wx.closeSocket({
      url: url,
      success: () => {
        console.log("socket close success")
      }
    })
  }
}
module.exports={
  createConnect: createConnect,
  closeSocket: closeSocket
  
}

@gongzili456

from weapp.socket.io.

niefang2017 avatar niefang2017 commented on May 22, 2024

调用

webSocket.createConnect({
      url: 'socket的url',
      room: room,
      response: (res) => {
        console.log("res:", res);
      },
      onLine: (msg) => {
        console.log("online:", msg)
      },
      onError: (err) => {

      },
      onClose: (e) => {

      },
      onSystem: (msg) => {
        console.log("system", msg)
        if (msg.type == 'update' && msg.success) {
          
        }
      }
    }) 

@gongzili456

from weapp.socket.io.

gongzili456 avatar gongzili456 commented on May 22, 2024

@niefang2017 本项目在内部管理力 websocket 的连接状态(open,close等),请不要再使用 wx API 管理连接,你不应该自己定义 closeSocket 方法来主动关闭连接。如需主动关闭请参考如下代码:

const socket = io(wx://example.com)
socket.disconnect()

from weapp.socket.io.

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.