Parse WeChat mini-app SocketIO instance

Source: Internet
Author: User
This article mainly introduces the relevant information of the small program SocketIO. Here we provide you with a simple example. if you need it, you can refer to the next article to introduce the relevant information of the small program SocketIO, A simple example is provided here. For more information, see

Simple example of the small program ScoketIO:

Currently, good people are engaged in small programs. ScoketIO is a network communication, and its importance is self-evident. here we will tell you how to use it and precautions!

The SocketIO implementation of the applet is improved based on the implementation of CFETram


Const emitter = require ('. /emitter. js');/** socket. io protocol constant */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. sendSocketMes Sage ({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 ('failed to parse the ws packet: ') 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

The DEMO project of a small program is provided to demonstrate accessing the official DEMO chat room of Scoket. IO for convenience of testing. for detailed usage, please refer to the official documentation.

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)}) where socket. in connect (ws_url, opts), the opts value is path, which is used to specify the socket. the default path for io. for example, set opts to the following values: {query: 'faneixiao', with: 'Mia & una ',}

The above section describes the details of the SocketIO instance. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.