You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
matrix-chat/api.js

44 lines
1.2 KiB
JavaScript

4 years ago
const ws = require('ws')
//WS server
const wss = new ws.Server({
port: 8090,
4 years ago
perMessageDeflate: {
zlibDeflateOptions: {
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
clientNoContextTakeover: true,
serverNoContextTakeover: true,
serverMaxWindowBits: 10,
concurrencyLimit: 10,
threshold: 1024
4 years ago
}
});
//WS handler
4 years ago
let user = [];
wss.on('connection', (ws, req) => {
console.log(`${req.socket.remoteAddress} connected`)
4 years ago
ws.on('message', msgJSON => {
let msg = JSON.parse(msgJSON)
console.log(`${req.socket.remoteAddress} => ${msgJSON}`)
if (msg.type === 'message') wss.clients.forEach(client => client.send(msgJSON))
4 years ago
else if (msg.type === 'login' && msg.content.user !== ""){
user.push(msg.content.user)
ws.send('{"type":"route","path":"/chat"}')
let msg = {type: "room", name: "open chat", user: user}
ws.send(JSON.stringify(msg))
}
4 years ago
})
4 years ago
let msg = {
type: "info",
time: Date.now(),
content: "connected"
}
ws.send(JSON.stringify(msg))
4 years ago
})