first commit

This commit is contained in:
编码猿
2024-09-27 01:09:52 +08:00
commit 0cf9eef5bf
149 changed files with 3476 additions and 0 deletions

1
socketEnd/utils/.pydio Normal file
View File

@@ -0,0 +1 @@
2c1850c2-c06e-472a-8537-aa75068498d9

View File

@@ -0,0 +1,92 @@
const Socket = require("ws");
const utils = require("./utils");
class WebSocket {
constructor(options) {
this.SocketList = {};
this.options = {};
this.WS = null;
this.options = options;
this.createServer()
}
createServer() {
const WS = new Socket.Server(this.options);
WS.on('connection', (ws, req) => this.connection(ws, req))
}
async connection(ws, req) {
console.log('建立连接:', req.url);
const id = req.url.split('/')[1];
if (!id) return ws.close();
this.SocketList[id] = ws;
ws.id = id;
this.filter();
ws.on('message', this.message.bind(this));
ws.on('close', (msg) => this.close(msg, ws.id));
}
async message(msg) {
msg = msg.toString('utf-8');
try {
const message = JSON.parse(msg);
switch (message.type) {
// 收到客户端1 android 的消息
// 需要 拿到到GPS的经纬度 进行距离判断
// 然后服务器 向客户端2 推送消息
case 1:
console.log("收到客户端1 android 的消息");
this.sendMessage({ data: "start" }, "clent2")
break;
// 收到客户端2的消息
case 2:
// this.send("flutter客户端2测试")
console.log("收到客户端2的消息", message);
break;
default:
break;
}
} catch (error) {
console.log(error);
console.log("消息格式必须为Object");
}
}
async filter() {
for(let key in this.SocketList) {
if (!this.SocketList[key].readyState === 1) delete this.SocketList[key]
}
}
close(msg, id) {
console.log('关闭连接', id, msg);
}
sendMessage(msg, id) {
// console.log("==================");
if(id) {
// console.log("if ==== ", msg, id);
// console.log("this.SocketList ==== : ", this.SocketList);
if (this.SocketList[id].readyState === 1) {
this.SocketList[id].send(JSON.stringify(msg));
}
} else {
// console.log("else");
// console.log("this.SocketList: ", this.SocketList);
for (let ws of Object.values(this.SocketList)) {
if (ws.readyState === 1) ws.send(JSON.stringify(msg));
}
}
}
}
module.exports = WebSocket;

34
socketEnd/utils/utils.js Normal file
View File

@@ -0,0 +1,34 @@
class utils {
/**
* 计算两经纬度点之间的距离(单位:米)
* @param {*} lat1 纬度1
* @param {*} lng1 经度1
*
* @param {*} lat2 纬度2
* @param {*} lng2 经度2
* @returns
*/
caculateLL(lng1, lat1, lng2 = 117.325426, lat2 = 31.794477) {
var radLat1 = (lat1 * Math.PI) / 180.0;
var radLat2 = (lat2 * Math.PI) / 180.0;
var a = radLat1 - radLat2;
var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
var s =
2 *
Math.asin(
Math.sqrt(
Math.pow(Math.sin(a / 2), 2) +
Math.cos(radLat1) *
Math.cos(radLat2) *
Math.pow(Math.sin(b / 2), 2)
)
);
s = s * 6378.137;
s = Math.round(s * 10000) / 10;
return s;
}
}
module.exports = new utils();