Last company quit, summarize the wheel made when working in the last company, and share it here by the way
Because the last company is related to the products of small program, using webScoket, today I will share the wheel made at that time here, the first post of the new man, just a beginner of small white, don’t like mercy ah…
Making the address
Second encapsulation on the scoket based on wechat small program
- Add observer mode to receive any scoket data across pages
- Support for back-end data interception. DataValidation method intercepts any data returned from the backend to determine whether the data was sent successfully or failed
- The scoket supports automatic reconnection, and you can set the reconnection interval
- A scoket can be created and connected immediately upon instantiation or manually
- The front and back end convention commands support parametric configuration
- Supports successful/failed scoket creation callback
- At present only oneself have measured casually, had better test oneself first before using, if have a problem can contact VX: B948993029
The config the field name | type | describe |
---|---|---|
Debug | Boolean | Whether to enable debugging. After debugging is enabled, debugging information about each phase is displayed. You can also add debugging information |
environment | Boolean | True Formal False Tests the server |
HttpSocketUrl | String | Official environment scoketuUrl |
HttpTestSocketUrl | String | Test environment scoketuUrl |
scoketHeader | Object | {‘content-type’: ‘application/json’} |
heartBeatWait | Number | Heartbeat interval unit per millisecond |
autoReconnectNumMax | Number | Automatic reconnection times 0 indicates that reconnection attempts have been made |
reconnectionInterval | Number | Automatic reconnection interval in milliseconds |
Iscreate | Boolean | Value: true The object is created and connected immediately upon instantiation value: false The object is not created and connected immediately upon instantiation and can be created and connected by calling the createScoket() method |
instructionCheck | Function(data) | The return value must match the key input during listening; otherwise, it will not be heard. After parsing the command returned by the back end, return the value back |
heartbeatConnection | Function(no return value)/Object | SendMsg ({action:4,chatMsg:null})} For example, 2: Object {action:4,chatMsg:null} |
dataValidation | Received data verification Verifies whether the data returned by the backend succeeds or fails. Success Return true Failure Return false | |
success | Function(data) | Scoket link created/opened successfully callback |
fail | Function(data) | Scoket link creation failed/open failed callback |
Use the sample
const scoket = require('./xxx/webScoket.js'); Const config = {Debug: false, // whether to enable debugging environment: true, //true formal false test server HttpSocketUrl: 'XXXXXXXXXXX ', // official environment HttpTestSocketUrl:' XXXXXXXX ', // test environment scoketHeader: {// scoketHeader 'content-type': 'application/json'}, heartBeatWait: 2000, // heartbeat interval ms autoReconnectNumMax: 2, // Automatic reconnection times 0 represents the number of times reconnectionInterval: 10000, // reconnection interval ms Iscreate: InstructionCheck :(data)=>{},// back end instructionCheck:(data)=>{}, DataValidation :(data)=>{},// data validation is used to identify whether the data returned by the back-end server is successful or not. (res) => {},// Scoket creation failed/open failed callback fail: (err) => {},// scoket creation failed/open failed callback}Copy the code
Scoket instance method
methods | parameter | describe |
---|---|---|
on | on(string,success,fail) | Wx.$scoket. On (‘ back end convention instruction ‘, success, fail: failed to receive data) |
once | on(string,success,fail) | Wx.$scoket. Once (‘ back end convention instruction ‘, ‘success’, fail: failed to receive data) |
off | on(string) | Unlisten wx.$scoket.off(‘ front and back convention directive ‘) |
sendMsg | sendMsg(object,success,fail) | Send message sendMsg(‘ data object to be sent to back end ‘, SUCCESS data received successfully, fail: data received failed) |
closeScoket | closeScoket() | Close the scoket |
wx.$scoket = new scoket(config); / / / / initializes the object instance listens to bind instruction wx. $scoket. On (' bind '/ / agreed instruction before and after the end, (res) = > {the console. The log (' receive success' res)}, (err) = > { $scoket. Once ('sayworld',(res)=>{console.log(' received successfully ',res)},(err)=>{console.log(' received successfully ',res)}) Console. log(' data error ',err)}) wx.$scoke. off('sayworld'// back end convention directive,); Wx.$scoket. SendMsg ({action:4, / / command chatMsg: null, / / the message body}, (res) = > {the console. The log (' send success, res)}, (err) = > {the console. The log (' failure ', err)}) / / close scoket links wx.$scoket.closeScoket()Copy the code