Github有一个经过重写的微信小程序SignalR的js类库
https://github.com/liangshiw/SignalRMiniProgram-Client
于是我把他改成支付宝小程序的版本,上面这个项目的核心代码基本没有变,只是小程序开放接口改了一下,在支付宝小程序就能跑起来了
把下面的js代码复制到你的支付宝小程序即可(使用方法在下面):
【代码】
1 const protocal = { 2 protocol: "json", 3 version: 1 4 }; 5 6 const MessageType = { 7 /** Indicates the message is an Invocation message and implements the {@link InvocationMessage} interface. */ 8 Invocation: 1, 9 /** Indicates the message is a StreamItem message and implements the {@link StreamItemMessage} interface. */ 10 StreamItem: 2, 11 /** Indicates the message is a Completion message and implements the {@link CompletionMessage} interface. */ 12 Completion: 3, 13 /** Indicates the message is a Stream Invocation message and implements the {@link StreamInvocationMessage} interface. */ 14 StreamInvocation: 4, 15 /** Indicates the message is a Cancel Invocation message and implements the {@link CancelInvocationMessage} interface. */ 16 CancelInvocation: 5, 17 /** Indicates the message is a Ping message and implements the {@link PingMessage} interface. */ 18 Ping: 6, 19 /** Indicates the message is a Close message and implements the {@link CloseMessage} interface. */ 20 Close: 7, 21 } 22 23 24 export class HubConnection { 25 26 constructor() { 27 this.openStatus = false; 28 this.methods = {}; 29 this.negotiateResponse = {}; 30 this.url = ""; 31 this.invocationId = 0; 32 this.callbacks = {}; 33 } 34 35 start(url, queryString) { 36 var negotiateUrl = url + "/negotiate"; 37 if (queryString) { 38 for (var query in queryString) { 39 negotiateUrl += (negotiateUrl.indexOf("?") < 0 ? "?" : "&") + (`${query}=` + encodeURIComponent(queryString[query])); 40 } 41 } 42 my.request({ 43 url: negotiateUrl, 44 method: "POST", 45 success: (res) => { 46 this.negotiateResponse = res.data; 47 this.startSocket(negotiateUrl.replace("/negotiate", "")); 48 }, 49 fail: (res) => { 50 console.error(`requrst ${url} error : ${res}`); 51 } 52 }); 53 } 54 55 startSocket(url) { 56 url += (url.indexOf("?") < 0 ? "?" : "&") + ("id=" + this.negotiateResponse.connectionId); 57 url = url.replace(/^http/, "ws"); 58 this.url = url; 59 if (this.openStatus) { 60 return; 61 } 62 63 console.log(url); 64 65 my.connectSocket({ 66 url: url 67 }) 68 69 my.onSocketOpen(res => { 70 console.log(`websocket connectioned to ${this.url}`); 71 this.sendData(protocal); 72 this.openStatus = true; 73 this.onOpen(res); 74 }); 75 76 my.onSocketClose(res => { 77 console.log(`websocket disconnection`); 78 this.openStatus = false; 79 this.onClose(res); 80 }); 81 82 my.onSocketError(res => { 83 console.error(`websocket error msg: ${res}`); 84 this.close({ 85 reason: res 86 }); 87 this.onError(res) 88 }); 89 90 my.onSocketMessage(res => this.receive(res)); 91 } 92 93 on(method, fun) { 94 95 let methodName = method.toLowerCase(); 96 if (this.methods[methodName]) { 97 this.methods[methodName].push(fun); 98 } else { 99 this.methods[methodName] = [fun]; 100 } 101 } 102 103 onOpen(data) { } 104 105 onClose(msg) { } 106 107 onError(msg) { } 108 109 close(data) { 110 if (d