Skip to content

Commit 4c7d787

Browse files
committed
socket fixup
1 parent f3d71ea commit 4c7d787

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

client/src/Hooks/useUpdaterSocket.hook.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ function useUpdaterSocket(updateData, auth) {
2626
})
2727
// eslint-disable-next-line react-hooks/exhaustive-deps
2828
}, [])
29+
2930
const debouncedUpdate = useDebouncedFunction(updateData, 200)
3031

3132
const socketOptions = {
3233
onOpen: (e) => {
3334
sendRegisterMsg()
34-
console.log("ws open", e)
35+
console.log("ws open")
3536
},
3637
onClose: (e) => {
37-
console.log("ws close", e)
38+
console.log("ws close")
3839
},
3940
onMessage: (e) => {
40-
console.log("ws updMessage", e)
41+
console.log("ws update message")
4142
debouncedUpdate()
4243
},
4344
onError: (e) => {
@@ -55,6 +56,8 @@ function useUpdaterSocket(updateData, auth) {
5556
sendMessage(msg)
5657
}
5758

59+
const sendUpdateMsgDebounced = useDebouncedFunction(sendUpdateMsg, 200)
60+
5861
function sendUpdateMsg() {
5962
sendMsg("update")
6063
}
@@ -63,7 +66,7 @@ function useUpdaterSocket(updateData, auth) {
6366
sendMsg("register")
6467
}
6568

66-
return [sendUpdateMsg]
69+
return [sendUpdateMsgDebounced]
6770
}
6871

6972
export default useUpdaterSocket

server/app.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ const app = express()
2323
app.use(express.json({ extended: true }))
2424

2525
app.get('/getIp', (req, res) => {
26-
const ip = getIp()
27-
res.status(200).json({ ip })
26+
getIp()
27+
.then((ip) => res.status(200).json({ ip }))
28+
.catch(() => res.status(500))
2829
})
2930

3031
/**
@@ -86,19 +87,22 @@ async function connectMongo(mongoUri) {
8687
/**
8788
* Вывод информации о сервере
8889
*/
89-
function logServerStart() {
90+
async function logServerStart() {
9091
const [logName, sBef, sAft] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
92+
const ip = await getIp()
9193
console.log(`\n${logName} has been started`)
9294
console.log(`${sBef} Local${sAft} http://localhost:${PORT}`)
93-
console.log(`${sBef} On Your Network${sAft} http://${getIp()}:${PORT}`, '\n')
95+
console.log(`${sBef} On Your Network${sAft} http://${ip}:${PORT}\n`)
9496
}
9597

9698
/**
9799
* Получение ip сервера
98100
*/
99101
function getIp() {
100-
for (let key in os.networkInterfaces()) {
101-
const addr = os.networkInterfaces()[key][1].address
102-
if (addr != undefined) return addr
103-
}
102+
return new Promise((res, rej) => {
103+
require('dns').lookup(os.hostname(), (err, addr) => {
104+
addr ? res(addr) : rej()
105+
err && console.log(err)
106+
})
107+
})
104108
}

server/socket/wss.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const WebSocket = require('ws')
88
function startWSS(port) {
99
let wsCount = 0
1010
const wsServer = new WebSocket.Server({ port })
11-
const wsCollection = { test: [] }
11+
const wsCollection = {}
1212
wsServer.on('connection', (wsClient) => {
1313
wsClient.num = ++wsCount
14+
1415
wsClient.on("message", (data) => {
1516
try {
1617
const { userId, target } = tryParce(data)
@@ -20,35 +21,28 @@ function startWSS(port) {
2021
let clients = getClients(userId)
2122
clients.push({
2223
num: wsClient.num,
23-
send: wsClient.send
24+
send: msg => wsClient.send(msg)
2425
})
2526
wsCollection[userId] = clients
26-
27-
console.log("\ncollection \n", wsCollection, '\n')
2827
}
2928

3029
if (target == "update") {
3130
getClients(wsClient.userId).forEach((wsc) => {
32-
if ((wsClient.num === undefined) || (wsClient.num !== wsc.num)) wsc.send(data)
31+
if ((wsClient.num === undefined) || (wsClient.num !== wsc.num)) {
32+
wsc.send(data)
33+
}
3334
})
3435
}
3536

36-
console.log("wsClient", wsClient.num, "messaged", target)
37-
} catch (e) {
38-
console.log("wsClient", wsClient.num, "error on messsage", e)
39-
}
37+
} catch (e) { }
4038
})
41-
wsClient.on("open", () => console.log("wsClient", wsClient.num, "opened"))
39+
4240
wsClient.on("close", () => {
4341
wsCollection[wsClient.userId] = getClients(wsClient.userId).filter((wsc) => {
4442
return (wsClient.num === undefined) || (wsClient.num !== wsc.num)
4543
})
46-
console.log("wsClient", wsClient.num, "closed")
4744
})
48-
wsClient.on("error", () => console.log("wsClient", wsClient.num, "error"))
49-
50-
console.log("WSS connection", wsClient.num)
51-
});
45+
})
5246
wsServer.on("close", () => console.log("WSS closed"))
5347
wsServer.on("error", () => console.log("WSS error"))
5448
wsServer.on("listening", () => console.log("WSS listening"))

0 commit comments

Comments
 (0)