知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
WebSocket使用
發(fā)表時(shí)間:2021-4-30
發(fā)布人:葵宇科技
瀏覽次數(shù):77
是什么
一種網(wǎng)絡(luò)通信協(xié)議(握手階段使用的是http 1.1),如果需要服務(wù)端主動(dòng)向客戶端推送信息可以使用它。
優(yōu)勢(shì)
全雙工,服務(wù)端和客戶端可以互通消息。
相對(duì)于各種論詢,不僅省掉多次握手消耗,節(jié)省帶寬資源,而且不用多次去詢問是否有新數(shù)據(jù)可消耗。
存在的問題
需要服務(wù)端配合,學(xué)習(xí)成本高些
如果需要獲取新數(shù)據(jù)的頻次少,一直保持鏈接的話,浪費(fèi)服務(wù)器資源。
使用方式(ps: 我使用的是本地nginx,自己生成的證書,密鑰)
技術(shù)方案:nodejs + nginx + 微信小程序
hosts文件配置:
127.0.0.1 localhost
127.0.0.1 www.test.com
服務(wù)端:
var express = require('express');
var ws = require('ws');
const http = require('http');
var server = http.createServer(express());
/**
* 創(chuàng)建websocket服務(wù)
*/
const wss = new ws.createServer({ server, path: '/wss' }, ws => {
serveMessage(ws);
});
/**
* 監(jiān)聽websocket服務(wù)錯(cuò)誤
*/
wss.on('error', (err) => {
console.log(err);
});
/**
* 進(jìn)行簡(jiǎn)單的 WebSocket 服務(wù),對(duì)于客戶端發(fā)來的所有消息都回復(fù)回去
*/
function serveMessage(ws) {
// 監(jiān)聽客戶端發(fā)來的消息
ws.on('message', (message) => {
console.log(`WebSocket received: ${message}`);
ws.send(`Server: Received(${message})`);
});
// 監(jiān)聽關(guān)閉事件
ws.on('close', (code, message) => {
console.log(`WebSocket client closed (code: ${code}, message: ${message || 'none'})`);
});
// 連接后馬上發(fā)送成功響應(yīng)
ws.send(`Server: 收到我的消息了嘛`);
}
server.listen(8888);
nginx配置(ps: 如果只是調(diào)試的話也可以使用80端口):
server {
server_name www.test.com;
listen 443;
ssl on;
ssl_certificate xxx.crt; # 證書
ssl_certificate_key yyy.key; # 密鑰,生成方式可自行搜索
access_log /usr/local/etc/nginx/access.log; # 記錄訪問日志
error_log /usr/local/etc/nginx/error.log; # 記錄錯(cuò)誤日志
proxy_read_timeout 10s; #設(shè)置超時(shí)時(shí)間,防止node服務(wù)就是掛的
proxy_connect_timeout 5s; #設(shè)置超時(shí)時(shí)間,防止node服務(wù)就是掛的
location /wss {
proxy_pass http://localhost:8888;
proxy_http_version 1.1; # 必需
proxy_set_header Upgrade $http_upgrade; # 必需
proxy_set_header Connection $connection_upgrade; # 必需
proxy_set_header Host $host; # 非必需
proxy_set_header X-Real-IP $remote_addr; # 非必需
proxy_set_header X-Real-Port $remote_port; # 非必需
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 非必需
proxy_set_header X-Forwarded-Protocol "$scheme"; # 非必需
}
location / {
proxy_pass http://localhost:999;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Protocol $scheme;
}
}
小程序客戶端(ps: 具體使用語法可以查看mp.weixin.qq.com官方小程序文檔):
wx.connectSocket({
url: 'wss://www.test.com/wss',
data: {
message: 'test'
},
success: function (res) {
console.log('成功了', res);
},
fail: function () {
console.log('失敗了');
}
});
wx.onSocketOpen(function (res) {
//balabala,可以向服務(wù)端發(fā)消息了
}
wx.onSocketMessage(function(data) {
//接收到服務(wù)端消息,balabala
}
wx.onSocketError(function (res) {
//socket錯(cuò)誤,balabala
}
遇到的問題
websocket 客戶端自動(dòng)關(guān)掉鏈接:原因是長(zhǎng)時(shí)間不和websocket服務(wù)端交流,超過了nginx設(shè)置的proxy_read_timeout時(shí)間就自動(dòng)關(guān)閉了,如果想長(zhǎng)時(shí)間鏈接,可以采用setTimeout等類似工具判斷斷了重鏈。
upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server:...: 如果nginx確保沒錯(cuò),多半是websocket server的原因,比如我的,解決方法就是把node中socket.io模塊換成了ws模塊。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南花農(nóng)小程序
- 昆明小程序開發(fā)
- 汽車回收系統(tǒng)
- 云南小程序被騙
- 開通微信小程序被騙
- 小程序被騙退款成功
- 網(wǎng)站沒排名
- 云南小程序制作
- 網(wǎng)站建設(shè)
- 云南網(wǎng)站建設(shè)報(bào)價(jià)
- 云南網(wǎng)站建設(shè)首選公司
- 英文網(wǎng)站建設(shè)公司
- 專業(yè)網(wǎng)站建設(shè)公司
- 網(wǎng)站建設(shè)方案 doc
- 云南網(wǎng)站建設(shè)特性
- 網(wǎng)站建設(shè)首頁
- 昆明網(wǎng)站制作
- 汽車報(bào)廢管理系統(tǒng)
- 定制小程序開發(fā)
- 網(wǎng)站排名優(yōu)化
- 云南小程序定制
- 昆明小程序定制開發(fā)
- 云南網(wǎng)站建設(shè)方法
- 網(wǎng)站建設(shè)首選
- 云南網(wǎng)站建設(shè)
- 楚雄網(wǎng)站建設(shè)公司
- 搜索引擎自然排名
- 云南網(wǎng)站建設(shè)公司地址
- 開發(fā)框架
- 曲靖小程序開發(fā)