1234567891011121314151617181920212223242526272829303132333435363738 |
- module.exports = class {
- constructor(serverURL) {
- this.onopen = () => {};
- this.onmessage = () => {};
- this.onclose = () => {};
- this.onerror = () => {};
- wx.connectSocket({
- url: serverURL
- });
- wx.onSocketOpen(() => {
- this.onopen();
- });
- wx.onSocketMessage(msg => {
- this.onmessage(msg);
- });
- wx.onSocketClose(() => {
- this.onclose();
- });
- wx.onSocketError(error => {
- this.onerror(error);
- });
- }
- send(data) {
- wx.sendSocketMessage({
- data
- });
- }
- close() {
- wx.closeSocket();
- }
- };
|