-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-server.js
48 lines (39 loc) · 1.17 KB
/
test-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const app = require('http').createServer()
const io = require('socket.io')(app);
const fs = require('fs');
const os = require('os');
const path = require('path');
const PORT = process.env.SOCKET_PORT || 8080;
console.info(`Starting socket server at ${PORT}`);
app.listen(PORT);
io.on('connection', function (socket) {
console.info('Connected', socket.id);
socket.on('sayHi', function(cb) {
cb({status: true, message: 'hi'});
});
socket.on('createFile', function(fileName, contents, cb) {
let temp = os.tmpdir();
let filePath = path.join(temp, fileName);
fs.writeFileSync(filePath, contents);
cb({status: true, path: filePath});
});
socket.on('shouldFail', function(cb) {
cb({status: false});
});
socket.on('xping', function(cb) {
socket.emit('xpong', '1', '2', '3');
cb({status: true});
});
socket.on('broadcastToAll', function(cb) {
io.emit('broadcastToAll', 'broadcasted!');
cb({status: true});
});
});
io.of('/clientNamespace').on('connection', function () {
console.log('Connected to /clientNamespace');
});
io.of('/failureNamespace').use(function(socket, next) {
next(new Error('Failed'));
}).on('connection', function() {
throw '/failureNamespace';
});