【node+小程序+web端】简单的websocket通讯

websoket是用来做什么的?

聊天室 消息列表 拼多多

即时通讯,推送, 实时交互

websoket是什么

websocket是一个全新的、独立的协议,基于TCP协议,与HTTP协议兼容却不会融入HTTP协议,仅仅作为HTML5的一部分。

HTTP是个懒惰的协议,server只有收到请求才会做出回应,否则什么事都不干。因此,为了彻底解决这个server主动像client发送数据的问题。W3C在HTML5中提供了一种client与server间进行全双工通讯的网络技术WebSocket。

node模块websocket和socket.io的区别

websocket只是socket.io实现业务封装的一个浏览器方面的backend,类比的话,websocket是tcp,而socket.io是http,后者固然基于前者,但是必须要找socket.io约定的protocol走

用socket.io简单的建立一个服务器

const http = require('http');
const io = require('socket.io'); // 创建http服务
let httpServer = http.createServer();
httpServer.listen(2333,()=>{
console.log('on port 2333')
});
// 创建websocket服务,将socket.io绑定到服务器上
let wsServer = io.listen(httpServer,()=>{
console.log('on port 2333')
}); wsServer.on('connection',function(sock){
sock.on('a',function(num1,num2){
console.log(`接到了浏览器发送的数据:${num1}`)
})
setInterval(function(){
sock.emit('ttt',Math.random())
},500)
})

用websocket搭建服务器

const http = require('http')
const WebSocketServer = require('websocket').server const httpServer = http.createServer((request, response) => {
console.log('[' + new Date + '] Received request for ' + request.url)
response.writeHead(404)
response.end()
}) const wsServer = new WebSocketServer({
httpServer,
autoAcceptConnections: true
}) wsServer.on('connect', connection => {
connection.on('message', message => {
if (message.type === 'utf8') {
console.log('>> message content from client: ' + message.utf8Data)
connection.sendUTF('[from server] ' + message.utf8Data)
}
}).on('close', (reasonCode, description) => {
console.log('[' + new Date() + '] Peer ' + connection.remoteAddress + ' disconnected.')
})
}) httpServer.listen(8111, () => {
console.log('[' + new Date() + '] Serveris listening on port 8080')
})

在html页面调用websocket

<html>
<body>
<head>
<script src="http://localhost:2333/socket.io/socket.io.js" charset="utf-8"></script>
<script>
let sock = io.connect('ws://localhost:2333')
document.onclick=function(){
sock.emit()
}
sock.on('ttt',function(n){
console.log(`接到了服务器发送的${n}`)
})
</script>
</head>
</body>
</html>

在小程序端调用请求

小程序websocket-api

 localsession: function(data){
wx.connectSocket({
url: 'ws://localhost:8999'
})
wx.onSocketOpen(function (res) {
console.log('WebSocket连接已打开!')
setInterval(()=>{
wx.sendSocketMessage({
data: data,
})
},3000)
}) wx.onSocketMessage(function (res) {
console.log(res)
}) wx.onSocketClose(function (res) {
console.log('WebSocket连接已关闭!')
})
},

WebSocket 与 Socket.IO

在vue中运用websocket

1.

vue-websocket

2.

vue-socket.io

3.

 let ws = new WebSocket('ws://192.168.1.205:9032/websocket');
ws.onopen = () => {
// Web Socket 已连接上,使用 send() 方法发送数据
//console.log('数据发送中...')
//ws.send('Holle')
//console.log('数据发送完成')
}
ws.onmessage = evt => {
console.log('数据已接收...')
var received_msg = evt.data;
console.log(received_msg); if("notice" == received_msg)
{
this.initData();
this.play();
}
else{
console.log("不刷新");
}
}
/* ws.onclose = function () {
// 关闭 websocket
console.log('连接已关闭...')
}
// 路由跳转时结束websocket链接
this.$router.afterEach(function () {
ws.close()
})*/

【node+小程序+web端】简单的websocket通讯的更多相关文章

  1. 小程序 web 端实时运行工具

    微信小程序 web 端实时运行工具 https://chemzqm.github.io/wept/

  2. 一套代码小程序&Web&Native运行的探索05——snabbdom

    接上文:一套代码小程序&Web&Native运行的探索04——数据更新 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/ma ...

  3. 一套代码小程序&Web&Native运行的探索03——处理模板及属性

    接上文:一套代码小程序&Web&Native运行的探索02 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/m ...

  4. 一套代码小程序&Web&Native运行的探索02

    接上文:一套代码小程序&Web&Native运行的探索01,本文都是一些探索性为目的的研究学习,在最终版输出前,内中的内容可能会有点乱 参考: https://github.com/f ...

  5. 一套代码小程序&Web&Native运行的探索04——数据更新

    接上文:一套代码小程序&Web&Native运行的探索03 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tree/master/m ...

  6. uniapp之uni-starter小程序多端研发框架搭建与项目实践

    随着移动互联网的飞速发展,无数移动APP琳琅满目:在移动App的发展的基础上,衍生了小程序.轻应用技术,它随时可用,但又无需安装卸载.小程序是一种不需要下载安装即可使用的应用,它实现了应用" ...

  7. 一套代码小程序&Web&Native运行的探索06——组件系统

    接上文:一套代码小程序&Web&Native运行的探索05——snabbdom 对应Git代码地址请见:https://github.com/yexiaochai/wxdemo/tre ...

  8. java实现微信小程序服务端(登录)

    微信小程序如今被广泛使用,微信小程序按照微信官网的定义来说就是: 微信小程序是一种全新的连接用户与服务的方式,它可以在微信内被便捷地获取和传播,同时具有出色的使用体验. 这就是微信小程序的魅力所在,有 ...

  9. 微信小程序 wxs的简单应用

    Demo地址:微信小程序wxs的简单应用 案例分析 张三.李四.王五,各自分别都有数量不等的车,现在需要列表显示名字及他们拥有车的数量, list数据结构如下,当我们使用wx:for进行显示时,发现个 ...

随机推荐

  1. Shell 常用的命令

    ls功能:列出目录内容常用选项:-a 显示所有文件,包括隐藏的-l 长格式列出信息-i 显示文件 inode 号-t 按修改时间排序-r 按修改时间倒序排序-h 打印易读大小单位 2 echo功能:打 ...

  2. charles撰写工具/compose和Compose New

    撰写工具/compose和Compose New compose 是在原有的请求基础上,修改: compose New 是新出一个弹窗,自己手动一个个的去写: 可以写各种状态:– URL:– Meth ...

  3. JavaScript -- Navigator

    -----040-Navigator.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&qu ...

  4. redis源码学习-dict

    1.字典相关的几个结构体 dict由hash table存储key-value, hash table数组每一个元素存放dictEntry链接的链表头结点,dictEntry节点存放key-value ...

  5. linux中为什么cpu使用率会超过100见解

    linux的cpu使用频率是根据cpu个数和核数决定的 top,然后你按一下键盘的1,这就是单个核心的负载,不然是所有核心的负载相加,自然会超过100 如上面 cpu个数是4个,那么cpu可以占到40 ...

  6. php的 $_REQUEST取值为空

    默认的 $_REQUEST 会获取 $_POST, $_GET, $_COOKIE的数据,这些可以通过查看 php.ini来确认: 由上图可以看出,获取的内容是通过 variables_order 和 ...

  7. 价值 1500 美元的 iPhone 值得买吗

    原文链接:价值 1500 美元的 iPhone 值得买吗 最新款 iPhone 的最高配型号在含税的情况下价格远超 1500 美元.价格合理吗?合理.理由如下:1,硬件已与笔记本电脑相当,价格也相当: ...

  8. Flutter踩坑日记:解除依赖

    Flutter已经融入工程有一段时间了,由于团队人数较少,所以一直没有管和原有工程解依赖的问题,今天有时间正好把这个问题给搞了. 一.分析 首先,直接忽略上一篇<接入现有iOS项目>的所有 ...

  9. (转)Spring常用注解

    使用注解来构造IOC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  10. Spring Aop AfterReturning接收返回值

    包结构: Spring.xml UserDao.java 测试类Main方法 LogAspect.java 测试结果: @AfterReturning标签属性分析: value值: 可以写Aop的表达 ...