相思资源网 Design By www.200059.com
微信小程序 ScoketIO 简单实例:
现在好的人在搞微信小程序,ScoketIO 是微信的网络通信,它的重要性不言而喻,这里给大家讲讲如何使用以及注意事项!
微信小程序 的SocketIO 实现,基于CFETram 的实现基础上完善
const emitter = require('./emitter.js'); /** socket.io 协议常量 */ var packets = { open: 0 // non-ws , close: 1 // non-ws , ping: 2 , pong: 3 , message: 4 , upgrade: 5 , noop: 6 }; var events = { CONNECT: 0, DISCONNECT: 1, EVENT: 2, ACK: 3, ERROR: 4, BINARY_EVENT: 5, BINARY_ACK: 6 }; const PING_CHECK_INTERVAL = 2000; class WxSocketIO { connect(url) { return new Promise((resolve, reject) => { wx.onSocketOpen((response) => { this.isConnected = true; //this.ping(); resolve(response); }); wx.onSocketError(error => { if (this.isConnected) { this.fire('error', error); } else { reject(error); } }); wx.onSocketMessage(message => this._handleMessage(message)); wx.onSocketClose(result => { if (this.isConnected) { this.fire('error', new Error("The websocket was closed by server")); } else { this.fire('close'); } this.isConnected = false; this.destory(); }); wx.connectSocket({ url: `${url}/"") }); } destory() { this.removeAllListeners(); } _handleMessage({ data }) { const [match, packet, event, content] = /^(\d)(\d"htmlcode">const opts = {} const socket = this.globalData.socket = new WxSocketIO() socket.connect('ws://chat.socket.io', opts) .then(_ => { console.info('App::WxSocketIO::onOpen') console.info('App:onShow:', that.globalData) }) .catch(err => { console.error('App::WxSocketIO::onError', err) }) 其中socket.connect(ws_url, opts)中,opts目前可选值是path,用来指定使用socket.io时默认的path,比如设置opts为下列值: { query: 'fanweixiao', with: 'mia&una', }完整实例地址:https://github.com/fanweixiao/wxapp-socket-io
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无微信小程序 SocketIO 实例讲解的评论...