这篇文章主要介绍了微信小程序 SocketIO 的相关资料,这里给大家提供了一个简单实例,需要的朋友可以参考下
微信小程序 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}/?EIO=3&transport=websocket` }); }); } ping() { setTimeout(() => { if (!this.isConnected) return; wx.sendSocketMessage({ data: [packets.ping, 'probe'].join('') }); }, PING_CHECK_INTERVAL); } close() { return new Promise((resolve, reject) => { if (this.isConnected) { this.isConnected = false; wx.onSocketClose(resolve); wx.closeSocket(); } else { reject(new Error('socket is not connected')); } }); } emit(type, ...params) { const data = [type, ...params]; wx.sendSocketMessage({ data: [packets.message, events.EVENT, JSON.stringify(data)].join("") }); } destory() { this.removeAllListeners(); } _handleMessage({ data }) { const [match, packet, event, content] = /^(\d)(\d?)(.*)$/.exec(data); if (+event === events.EVENT) { switch (+packet) { case packets.message: let pack; try { pack = JSON.parse(content); } catch (error) { console.error('解析 ws 数据包失败:') console.error(error); } const [type, ...params] = pack; this.fire(type, ...params); break; } } else if (+packet == packets.pong) { this.ping(); } } }; emitter.setup(WxSocketIO.prototype); module.exports = WxSocketIO;
DEMO
项目附了一个微信小程序的DEMO 项目演示了接入 Scoket.IO 官方的演示聊天室,以便方便测试,关于详细用法还请参考官方文档。
How to use
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', }
以上是解析微信小程序SocketIO实例的详细内容。更多信息请关注PHP中文网其他相关文章!
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前ByDDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能