├── app.wxss
├── index
├── index.json
├── index.wxss
├── index.wxml
└── index.js
├── app.js
├── sitemap.json
├── README.md
├── app.json
└── project.config.json
/app.wxss:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/index/index.json:
--------------------------------------------------------------------------------
1 | {
2 | "usingComponents": {}
3 | }
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | App({
2 | onLaunch: function () {
3 |
4 | }
5 | })
6 |
--------------------------------------------------------------------------------
/index/index.wxss:
--------------------------------------------------------------------------------
1 | .intro {
2 | margin: 30px;
3 | text-align: center;
4 | }
--------------------------------------------------------------------------------
/index/index.wxml:
--------------------------------------------------------------------------------
1 | {{messages}}
2 |
3 |
--------------------------------------------------------------------------------
/sitemap.json:
--------------------------------------------------------------------------------
1 | {
2 | "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
3 | "rules": [{
4 | "action": "allow",
5 | "page": "*"
6 | }]
7 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WeChatMiniAppSignalRClient
2 | 微信小程序 Asp.net Core SignalR Client 代码片段演示
3 |
4 | 服务端麻烦自己搭
5 |
6 | 服务端源码:https://github.com/lishewen/SignalRSimpleSample
7 |
8 | # 代码片段分享链接
9 | https://developers.weixin.qq.com/s/IDLPCnm87W8u
10 |
--------------------------------------------------------------------------------
/app.json:
--------------------------------------------------------------------------------
1 | {
2 | "pages": [
3 | "index/index"
4 | ],
5 | "window": {
6 | "backgroundTextStyle": "light",
7 | "navigationBarBackgroundColor": "#fff",
8 | "navigationBarTitleText": "WeChat",
9 | "navigationBarTextStyle": "black"
10 | },
11 | "sitemapLocation": "sitemap.json"
12 | }
--------------------------------------------------------------------------------
/project.config.json:
--------------------------------------------------------------------------------
1 | {
2 | "description": "项目配置文件。",
3 | "packOptions": {
4 | "ignore": []
5 | },
6 | "setting": {
7 | "urlCheck": false,
8 | "es6": true,
9 | "postcss": true,
10 | "minified": true,
11 | "newFeature": true,
12 | "uglifyFileName": true,
13 | "checkInvalidKey": true,
14 | "checkSiteMap": true,
15 | "uploadWithSourceMap": true,
16 | "babelSetting": {
17 | "ignore": [],
18 | "disablePlugins": [],
19 | "outputPath": ""
20 | },
21 | "enhance": true
22 | },
23 | "compileType": "miniprogram",
24 | "libVersion": "2.7.0",
25 | "appid": "touristappid",
26 | "projectname": "SignalR",
27 | "simulatorType": "wechat",
28 | "simulatorPluginLibVersion": {},
29 | "condition": {
30 | "search": {
31 | "current": -1,
32 | "list": []
33 | },
34 | "conversation": {
35 | "current": -1,
36 | "list": []
37 | },
38 | "game": {
39 | "currentL": -1,
40 | "list": []
41 | },
42 | "miniprogram": {
43 | "current": -1,
44 | "list": []
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/index/index.js:
--------------------------------------------------------------------------------
1 | const app = getApp()
2 |
3 | Page({
4 | RecordSeparatorCode: 0x1e,
5 | inputValue: '',
6 | data: {
7 |
8 | },
9 | bindKeyInput(e) {
10 | this.inputValue = e.detail.value;
11 | },
12 | sendMessage() {
13 | let self = this;
14 | let RecordSeparator = String.fromCharCode(self.RecordSeparatorCode);
15 | let body = {
16 | arguments: [{
17 | message: self.inputValue,
18 | sent: new Date()
19 | }],
20 | target: 'SendMessage',
21 | type: 1,
22 | };
23 | let senddata = `${JSON.stringify(body)}${RecordSeparator}`;
24 | console.log(senddata);
25 | wx.sendSocketMessage({
26 | data: senddata,
27 | });
28 | },
29 | connectSignalR() {
30 | let self = this;
31 | let RecordSeparator = String.fromCharCode(self.RecordSeparatorCode);
32 | wx.connectSocket({
33 | url: "wss://jbwx.lishewen.com/testmessages",
34 | });
35 | wx.onSocketOpen(() => {
36 | console.log('连接成功');
37 | let handshakeRequest = {
38 | protocol: 'json',
39 | version: 1
40 | };
41 | let senddata = `${JSON.stringify(handshakeRequest)}${RecordSeparator}`;
42 | wx.sendSocketMessage({
43 | data: senddata,
44 | });
45 | });
46 | wx.onSocketMessage(res => {
47 | try {
48 | let jsonstrs = String(res.data).split(RecordSeparator);
49 | jsonstrs.forEach(jsonstr => {
50 | if (jsonstr) {
51 | let obj = JSON.parse(jsonstr);
52 | if (obj.type == 1 && obj.target == 'Send') {
53 | let messages = obj.arguments[0].message;
54 | //console.log(messages);
55 | self.setData({
56 | messages: messages
57 | });
58 | }
59 | }
60 | });
61 | } catch (ex) {
62 | console.log('异常:' + ex);
63 | console.log('收到服务器内容:' + res.data);
64 | }
65 | });
66 | wx.onSocketError(() => {
67 | console.log('websocket连接失败!');
68 | });
69 | },
70 | onLoad() {
71 | this.connectSignalR();
72 | },
73 | })
--------------------------------------------------------------------------------