最近需要实现一个功能,后端通过TCP协议连接雷达硬件的控制器,前端通过websocket连接后端,当控制器触发消息的时候,把信息通知给所以前端;

第一个思路是单独写一个后端服务用来实现websocket,调试成功了,后来又发现一个插件express-ws,于是决定改变思路,研究了下,最终代码如下,希望帮助更多的朋友,不再害怕websocket

首先写一个前端websocket服务。这里我选择放弃单例模式,采用谁调用谁负责销毁的思路

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { LoginService } from '../login/login.service';
import { environment } from 'src/environments/environment'; export class WsConnect {
ws!:WebSocket;
sendWs!:(msg:string)=>void;
closeWs!:()=>void;
result!:Observable<any>
}
@Injectable({providedIn:"root"})
export class WebsocketService { origin = window.location.origin.replace('http', 'ws'); constructor(
private loginService: LoginService
) { } getUrl(path:string){
return `${this.origin}${path}`;
}
connect(path:string):WsConnect{
let url = this.getUrl(path);
let ws = new WebSocket(url, this.loginService.userInfo.jwt); // 在这里放入jwt信息,目前没有找到其它地方可以放。有些网友建议先放入地址,然后在nginx里重新放入header,我觉得不够接地气
return {
ws,
sendWs:function(message:string){
ws.send(message);
},
closeWs:function(){
ws.close();
},
result:new Observable(
observer => {
ws.onmessage = (event) => { observer.next(event.data)};//接收数据
ws.onerror = (event) => {console.log("ws连接错误:",event);observer.error(event)};//发生错误
ws.onclose = (event) => {console.log("ws连接断开:",event); observer.complete() };//结束事件
ws.onopen = (event) => { console.log("ws连接成功:",event);};//结束事件
}
)
}
}
}

然后在组件里调用

import { Component, OnDestroy, OnInit } from '@angular/core';

import { WebsocketService, WsConnect } from '../utils/websocket-client.service';
@Component({
selector: 'app-car-measure',
templateUrl: './car-measure.component.html',
styleUrls: ['./car-measure.component.scss']
})
export class CarMeasureComponent implements OnInit , OnDestroy{
connect!:WsConnect;
constructor(public wsService:WebsocketService) { } ngOnInit() {
this.connectServer();
} connectServer(){
this.connect = this.wsService.connect('/websocket/carMeasure')
this.connect.result.subscribe(
(data:any) => { //接收到服务端发来的消息
console.log("服务器消息:",data);
setTimeout(() => {
this.connect.sendWs("这是从客户端发出的消息");
}, 5000);
}
)
} ngOnDestroy() {
this.connect.closeWs(); // 这个方法时把整个ws销毁,而不是取消订阅哦,所以有需要的同学可以考虑取消订阅的方案
}
}

后端引入express-ws,封装一个可调用的文件,部分代码借鉴了网上的代码,做了一些改善

//websocket.js
const express = require('express');
const router = express.Router();
const expressWs = require('express-ws')
// 初始化
let WS = null;
// 声明一个通道类
let channels = null;
let pathList = [
'/websocket/carMeasure',
'/path2'
]
function initWebSocket(app) {
WS = expressWs(app) //混入app, wsServer 存储所有已连接实例
// 创建通道
channels = new channel(router)
pathList.forEach(path=>{
channels.createChannel(path)
// channels.createChannel('/carMeasure/websocket/carSize')
})
app.use(router)
}
// 通道类
class channel {
router;
constructor(props) {
this.router = props;
}
createChannel(path) {
// 建立通道
this.router.ws( path, (ws, req) => {
//把自定义信息加入到socket里面取,expressws会自动放入到从WS.getWss().clients,
// 并且会自动根据活动用户删除或者增加客户端
ws['wsPath'] = path;
ws['userId'] = req.userInfo._id;
ws['roleId'] = req.userInfo.role;
ws.on('message', (msg) => getMsg(msg, path))
ws.on('close', (code) => close(code, path))
ws.on('error', (e) => error(e, path))
})
}
}
/**
*
* @param {*} msg 消息内容
* @param {String} from 消息来源
*/
// 监听消息
let getMsg = (msg, from) => {
console.log(msg, from);
// SendMsgAll({path:'/path2', data: msg })
}
// 发送消息
let sendMsg = (client, data) => {
if (!client) return
client.send(JSON.stringify(data))
}
let close = (code) => {
console.log('关闭连接', code);
}
let error = (e) => {
console.log('error: ', e);
}
// 群发
/**
*
* @param {String} path 需要发送的用户来源 路由,默认全部
* @param {*} data 发送的数据
*/
function sendMsgToClients(clients,data){
clients.forEach((client)=> {
if (client._readyState == 1) {
sendMsg(client, data)
}
})
} function sendMsgToAll(data = "") {
let allClientsList = Array.from(WS.getWss().clients)
sendMsgToClients(allClientsList,data)
} function sendMsgToPath(data = "", path = '') {
let allClientsList = Array.from(WS.getWss().clients).filter((ws)=>ws['wsPath'] == path)
sendMsgToClients(allClientsList,data)
}
function sendMsgToId(data = "", userId = '') {
let allClientsList = Array.from(WS.getWss().clients).filter((ws)=>ws['userId'] == userId)
sendMsgToClients(allClientsList,data)
}
function sendMsgToRole(data = "", roleId = '') {
let allClientsList = Array.from(WS.getWss().clients).filter((ws)=>ws['roleId'] == roleId)
sendMsgToClients(allClientsList,data)
}
module.exports = {
initWebSocket,
sendMsgToAll,
sendMsgToPath,
sendMsgToId,
sendMsgToRole,
}

然后再app.js里面调用就可以了

const {initWebSocket} = require('./public/utils/websocket')
initWebSocket(app)

其中涉及到了权限验证的问题,也可以直接验证jwt

app.use((req,res,next) => {
if(!whiteList.some(item => req.url.startsWith(item))) {
let httpJwt= req.headers['jwt'];
let wsJwt= req.headers['sec-websocket-protocol']; // 这里验证websocket的身份信息,其它代码
utils.verifyToken(httpJwt || wsJwt).then(res => { //utils.verifyToken封装了jwt的验证
req["userInfo"] = res; //放入一些信息,方便后续操作
          next()
}).catch(e => {
console.error(e);
res.status(401).send('invalid token')
})
} else {
next()
}
})

万事具备,最后一步就是等待硬件设备的触发了,其它tcp客户端的代码就不放出来干扰大家了,就是粗暴的调用即可

var {sendMsgToPath} = require('../public/utils/websocket');
sendMsgToPath(JSON.stringify(result), this.carMeasurePath);   // 注意websocket或者tcp的传输都只能用字符串或者blob

另外注意要配置nginx代理,nginx的配置各位应该都清楚吧,这里就不多说了,注意的是这里有几个可选择的地方,一个是前端,可以把ws服务做成单例,另一个是后端路由其实可以写在http的路由文件里,还有一个是对后端ws client的使用,利用了express-ws自身的方法,当然也可以自己写对象来搜集clients (不太建议)

想了以下还是放出来给小白,这里是proxy.config.json

{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true,
"pathRewrite": {
"^/api": "/"
}
},
"/websocket":{
"target": "http://localhost:3000",
"secure": false,
"ws": true
}
}

毕竟讲究的是手把手把你教会,不会也得会,这里是放入服务器的nginx.cong

worker_processes  1;

events {
worker_connections 1024;
} http {
server {
listen 80;
server_name localhost;
client_max_body_size 20M;
underscores_in_headers on; include /etc/nginx/mime.types; gzip on;
gzip_static on;
gzip_min_length 1000;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; location /{
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
} location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://localhost:3000; }
location /websocket {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}

最后,祝大家工作顺利,请记住 ‘耳机大神’ 永远陪着你

 

angular + express 实现websocket通信的更多相关文章

  1. beetle.express针对websocket的高性能处理

    客户需要对websocket服务应用,其要求每秒同时给3W个在线的websocket连接进行广播消息.以下针对beetle.express扩展websocket简单的性能测试.从测试结果来看基本没什么 ...

  2. C#(SuperWebSocket)与websocket通信

    原文:C#(SuperWebSocket)与websocket通信 客户端代码 点击可以查看一些关于websocket的介绍 <!DOCTYPE html> <html> &l ...

  3. js判断是否安装某个android app,没有安装下载该应用(websocket通信,监听窗口失去焦点事件)

    现在经常有写场景需要提示用户下载app, 但是如果用户已经安装,我们希望是直接打开app. 实际上,js是没有判断app是否已经安装的方法的,我们只能曲线救国. 首先,我们需要有call起app的sc ...

  4. Express框架Fetch通信

    最近自己弄个博客站点,前台用的React,服务器用的是node实现的,node是第一次接触,所以还在摸索,这篇mark下通信时遇到的坑. fetch配置: window.fetchUtility = ...

  5. Springboot集成WebSocket通信全部代码,即扣即用。

    websocket通信主要来自两个类以及一个测试的html页面. MyHandler 和 WebSocketH5Config,下面全部代码 MyHandler类全部代码: package com.un ...

  6. 【Java Web开发学习】Spring MVC整合WebSocket通信

    Spring MVC整合WebSocket通信 目录 ========================================================================= ...

  7. websocket通信1009错误,

    问题说明: springboot继承 WebSocketConfigurer实现websocket通信服务,服务器端报错,"The decoded text message was too ...

  8. Python3+WebSockets实现WebSocket通信

    一.说明 1.1 背景说明 前段时间同事说云平台通信使用了个websocket的东西,今天抽空来看一下具体是怎么个通信过程. 从形式上看,websocket是一个应用层协议,socket是数据链路层. ...

  9. webSocket通信

    针对webSocket通信总结: 1.webSocket通信原理图: 2.webSocket通信实例 参考地址1:https://www.cnblogs.com/cjm123/p/9674506.ht ...

  10. angular组件间的通信(父子、不同组件的数据、方法的传递和调用)

    angular组件间的通信(父子.不同组件的数据.方法的传递和调用) 一.不同组件的传值(使用服务解决) 1.创建服务组件 不同组件相互传递,使用服务组件,比较方便,简单,容易.先将公共组件写在服务的 ...

随机推荐

  1. 10.5. 版本控制(如Git)

    版本控制系统(Version Control System,VCS)是软件开发过程中用于管理源代码的工具.它可以帮助你跟踪代码的变更历史,方便回滚到之前的版本,以及协同多人共同开发.Git是当前最流行 ...

  2. 关于Unity3D第一视角下镜头穿墙的问题解决方法

    昨天做室内模型的时候,遇到一个非常棘手的问题,那就是第一视角在室内运行的时候,会出现穿墙的效果.类似下图效果,在靠近墙壁的时候,出现了镜头看见了墙壁外的情况,很显然这是不符合逻辑的.我们要做的就是避免 ...

  3. 基于Node.js的分布式应用程序架构设计与最佳实践:实现高效、可扩展的分布式系统

    目录 基于Node.js的分布式应用程序架构设计与最佳实践:实现高效.可扩展的分布式系统 随着互联网的普及和发展,分布式系统已经成为现代应用程序中不可或缺的一部分.而Node.js作为当前最流行的Ja ...

  4. ModifyAjaxResponse,修改ajax请求返回值,前后端调试之利器

    一.概要 先看图 京豆多的离谱,你的第一想法肯定是:按F12修改了网页元素 没那么简单,你看支持刷新的 肯定还是假的,通过 Fiddler 或 Wireshark 等抓包工具修改了响应包:或者干脆改了 ...

  5. javaSE 温故而知新

    重温 javaSE 前言:有地基才能有高楼大厦 目录 重温 javaSE 认识java Java基础 1.数据类型 1.1 基本数据类型: 1.2 引用数据类型 1.3 基本数据类型的包装类 1.4 ...

  6. 华为云河图KooMap 共筑数字孪生底座 共建产业标杆

    摘译:7月7日至9日,华为开发者大会2023(Cloud)将在东莞溪村盛大举行,由华为云河图KooMap带来的关于数字孪生主题的技术分论坛.圆桌闭门会和精彩成果展示 7月7日至9日,华为开发者大会20 ...

  7. ASL芯片CS5466方案设计|集睿致远CS5466代理商|Type-c转HDMI电路原理

    CS5466作为ASL集睿致远新推出的高性能Type-C to HDMI2.1协议转换器,可以通过HDMI输出端口作为TMDS或FRL发射机进行操作. CS5466适配于多个配件市场和现实应用主板,例 ...

  8. 我用numpy实现了GPT-2,GPT-2源码,GPT-2模型加速推理,并且可以在树莓派上运行,读了不少hungging face源码,手动实现了numpy的GPT2模型

    之前分别用numpy实现了mlp,cnn,lstm和bert模型,这周顺带搞一下GPT-2,纯numpy实现,最重要的是可在树莓派上或其他不能安装pytorch的板子上运行,生成数据 gpt-2的ma ...

  9. 从钢铁行业数字化管控与超自动化融合,看华为WeAutomate能力进化

    文/王吉伟 钢铁行业的数字化转型,历来都是值得探讨的热点话题. 2022年,我国粗钢产量10.13亿吨,占据了全球钢铁供给市场的半壁江山. 这组数据可谓非常抢眼,但仍旧难掩诸多企业的各种经营问题. 钢 ...

  10. Spring 的依赖注入

    Spring 的依赖注入 @ 目录 Spring 的依赖注入 每博一文案 1. 依赖注入 1.1 构造注入 1.1.1 通过参数名进行构造注入 1.1.2 通过参数的下标,进行构造注入 1.1.3 不 ...