first commit
This commit is contained in:
1
socketEnd/.pydio
Normal file
1
socketEnd/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
3cc42d59-bf43-40d6-9b77-9cd98018af9b
|
||||
41
socketEnd/index.html
Normal file
41
socketEnd/index.html
Normal file
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>websocket</title>
|
||||
</head>
|
||||
<body>
|
||||
<button>点我唤醒手机</button>
|
||||
|
||||
</body>
|
||||
<script type="text/javascript">
|
||||
// 浏览器提供 WebSocket 对象
|
||||
var ws = new WebSocket("ws://120.27.144.174:8080/html");
|
||||
|
||||
// 发送
|
||||
ws.onopen = function () {
|
||||
console.log("Connection open ...");
|
||||
};
|
||||
|
||||
ws.onmessage = function (evt) {
|
||||
console.log("Received Message: " + evt.data);
|
||||
};
|
||||
|
||||
document.querySelector("button").onclick = function () {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 1,
|
||||
data: {
|
||||
|
||||
// 大地中学 东北门
|
||||
// Longitude: 117.325131,
|
||||
// Latitude: 31.793141,
|
||||
|
||||
// 信旺华府俊苑14栋
|
||||
// Longitude: 117.237326,
|
||||
// Latitude: 31.830978,
|
||||
},
|
||||
})
|
||||
);
|
||||
};
|
||||
</script>
|
||||
</html>
|
||||
6
socketEnd/index.js
Normal file
6
socketEnd/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const WebSocket = require("./utils/WebSocket");
|
||||
|
||||
|
||||
new WebSocket({ port: 8080 });
|
||||
|
||||
|
||||
20
socketEnd/package.json
Normal file
20
socketEnd/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "socketEnd",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "node-dev index.js",
|
||||
"start": "pm2 start ./index.js --name auto --watch -i max"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ws": "^8.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"bufferutil": "^4.0.3",
|
||||
"utf-8-validate": "^5.0.5"
|
||||
}
|
||||
}
|
||||
1
socketEnd/utils/.pydio
Normal file
1
socketEnd/utils/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
2c1850c2-c06e-472a-8537-aa75068498d9
|
||||
92
socketEnd/utils/WebSocket.js
Normal file
92
socketEnd/utils/WebSocket.js
Normal 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
34
socketEnd/utils/utils.js
Normal 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();
|
||||
Reference in New Issue
Block a user