当前位置:网站首页>微信小程序WebSocket使用案例
微信小程序WebSocket使用案例
2022-06-27 05:13:00 【天马3798】
一、Asp.Net Core 6服务器端代码
服务器端WebScoket代码同上一篇博文:
Asp.Net Core6 WebSocket 简单案例_天马3798的博客-CSDN博客
二、微信小程序端WebSocket使用整理
1.wx.connectSocet() 创建 socket连接任务
详细地址:
2.SocketTask 监听处理 socket任务
3.小程序WebScoket并发数量
- 1.7.0 及以上版本,最多可以同时存在 5 个 WebSocket 连接。
- 1.7.0 以下版本,一个小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。
三、微信小程序端websocket代码使用案例
1.wxml
<text>pages/wx/test1/test1.wxml</text>
<input type="text" />
<button bindtap="sendClick">发送内容</button>
<view wx:for="{
{result}}">
{
{item}}
</view>2.js
var socket = null; //全局定义socket对象
Page({
/**
* 页面的初始数据
*/
data: {
result: []
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
var _this = this;
//创建websocket
//正式地址使用wss
socket = wx.connectSocket({
url: 'ws://localhost:5032/wstest/one',
success: res => {
console.info('创建连接成功');
//socketTaskId: 22
// console.info(res);
}
});
// console.info(socket);
//事件监听
socket.onOpen(function () {
console.info('连接打开成功');
});
socket.onClose(function () {
console.info('连接关闭成功');
});
socket.onError(function () {
console.info('连接报错');
});
//服务器发送监听
socket.onMessage(function (e) {
console.info(e);
var data = e.data;
var list = _this.data.result;
list = list.concat([data]);
_this.setData({
result: list
});
});
},
//发送事件
sendClick() {
socket.send({
data: '客户端测试内容',
success: res => {
console.info('客户端发送成功');
}
});
}
})3.运行效果

更多:
Asp.Net Core6 WebSocket 简单案例
ASP.NET Core5.0 SignalR CORS 跨域处理
Asp.Net Core6 WebSocket绑定
边栏推荐
猜你喜欢

How pychart installs packages

How JQ gets the reciprocal elements
![[nips 2017] pointnet++: deep feature learning of point set in metric space](/img/3e/0a47eecc27f236d629c611e683b37a.png)
[nips 2017] pointnet++: deep feature learning of point set in metric space

Redis高可用集群(哨兵、集群)

【Unity】UI交互组件之按钮Button&可选基类总结
![Mechanical transcoding journal [17] template, STL introduction](/img/78/926db660139fda3d31cceccad7096c.png)
Mechanical transcoding journal [17] template, STL introduction
![[station B up dr_can learning notes] Kalman filter 3](/img/40/d3ec97be2f29b76a6c049c26ff4998.gif)
[station B up dr_can learning notes] Kalman filter 3

【B站UP DR_CAN学习笔记】Kalman滤波1

Microservice system design -- microservice invocation design

【C语言】关键字的补充
随机推荐
Microservice system design -- microservice monitoring and system resource monitoring design
轨道姿态常用编程缩写
Interview: what are the positioning methods in selenium? Which one do you use most?
STM32关闭PWM输出时,让IO输出固定高或低电平的方法。
[station B up dr_can learning notes] Kalman filter 2
【C语言】关键字的补充
《数据库原理与应用》第一版 马春梅……编著 期末复习笔记
015 basics of C language: C structure and common body
pycharm 如何安装 package
Pycharm 中 Terminal 无法进入 venv 环境的问题
【FPGA】 基于FPGA分频,倍频设计实现
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
Microservice system design -- unified authentication service design
Neo4j community conflicts with neo4j desktop
stm32读取IO高低电平状态
微服务系统设计——统一鉴权服务设计
[622. design cycle queue]
Laptop does not have WiFi option solution
012 C语言基础:C数组
Machunmei, the first edition of principles and applications of database... Compiled final review notes