当前位置:网站首页>Wechat applet websocket use case

Wechat applet websocket use case

2022-06-27 05:17:00 Tianma 3798

One 、Asp.Net Core 6 Server-side code

Server side WebScoket The code is the same as the previous blog post :

Asp.Net Core6 WebSocket A simple case _ Tianma 3798 The blog of -CSDN Blog

Two 、 Wechat applet WebSocket Use finishing

1.wx.connectSocet() establish socket Connect tasks

Detailed address :

SocketTask | Wechat open documents

2.SocketTask Monitor processing socket Mission

SocketTask | Wechat open documents

3. Applet WebScoket Concurrent number

  • 1.7.0 And above , Can exist at most at the same time 5 individual WebSocket Connect .
  • 1.7.0 The following versions , A small program can only have one WebSocket Connect , If there is already a WebSocket Connect , Will automatically close the connection , And recreate a WebSocket Connect .

3、 ... and 、 Wechat applet websocket Code use cases

1.wxml

<text>pages/wx/test1/test1.wxml</text>

<input type="text" />

<button bindtap="sendClick"> send content </button>

<view wx:for="{
   {result}}">
{
   {item}}
</view>

2.js

var socket = null; // Global definition socket object 
Page({
    /**
     *  Initial data of the page 
     */
    data: {
        result: []
    },
    /**
     *  Life cycle function -- Monitor page loading 
     */
    onLoad(options) {
        var _this = this;
        // establish websocket
        // Use of official address wss
        socket = wx.connectSocket({
            url: 'ws://localhost:5032/wstest/one',
            success: res => {
                console.info(' Connection created successfully ');
                //socketTaskId: 22
                // console.info(res);
            }
        });
        // console.info(socket);
        // Event monitoring 
        socket.onOpen(function () {
            console.info(' Connection opened successfully ');
        });
        socket.onClose(function () {
            console.info(' Connection closed successfully ');
        });
        socket.onError(function () {
            console.info(' Connection error ');
        });
        // The server sends listening 
        socket.onMessage(function (e) {
            console.info(e);
            var data = e.data;
            var list = _this.data.result;
            list = list.concat([data]);
            _this.setData({
                result: list
            });
        });
    },
    // Send events 
    sendClick() {
        socket.send({
            data: ' Client test content ',
            success: res => {
                console.info(' The client sent successfully ');
            }
        });
    }
})

3. Running effect

 

more :

Asp.Net Core6 WebSocket A simple case

ASP.NET Core5.0 SignalR CORS Cross domain processing

Asp.Net Core6 WebSocket binding

原网站

版权声明
本文为[Tianma 3798]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270512439028.html