You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
55 lines
1.2 KiB
var request = require('supertest'); |
|
request = request('http://127.0.0.1:8080'); |
|
const WebSocket = require('ws'); |
|
|
|
async function registerChat() { |
|
try { |
|
const response = await request.post('/api/chat/register'); |
|
return response.body; |
|
} catch (e) { |
|
console.error(e); |
|
} |
|
} |
|
|
|
function sendChatMessage(message, accessToken, done) { |
|
const ws = new WebSocket( |
|
`ws://localhost:8080/ws?accessToken=${accessToken}`, |
|
{ |
|
origin: 'http://localhost:8080', |
|
} |
|
); |
|
|
|
function onOpen() { |
|
ws.send(JSON.stringify(message), function () { |
|
ws.close(); |
|
done(); |
|
}); |
|
} |
|
|
|
ws.on('open', onOpen); |
|
} |
|
|
|
async function listenForEvent(name, accessToken, done) { |
|
const ws = new WebSocket( |
|
`ws://localhost:8080/ws?accessToken=${accessToken}`, |
|
{ |
|
origin: 'http://localhost:8080', |
|
} |
|
); |
|
|
|
ws.on('message', function incoming(message) { |
|
const messages = message.split('\n'); |
|
messages.forEach(function (message) { |
|
const event = JSON.parse(message); |
|
|
|
if (event.type === name) { |
|
done(); |
|
ws.close(); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
module.exports.sendChatMessage = sendChatMessage; |
|
module.exports.registerChat = registerChat; |
|
module.exports.listenForEvent = listenForEvent;
|
|
|