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.
130 lines
3.0 KiB
130 lines
3.0 KiB
var path = require('path');
|
|
var app = require('express')();
|
|
var http = require('http').Server(app);
|
|
var io = require('socket.io')(http);
|
|
|
|
const socketAuth = require('socketio-auth');
|
|
|
|
|
|
const cors = require('cors')
|
|
const whitelist = [
|
|
'http://localhost:3080',
|
|
'http://localhost:3081',
|
|
'http://localhost:3082'
|
|
]
|
|
const corsOptions = {
|
|
credentials: true,
|
|
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
|
|
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
|
|
allowedHeaders: [
|
|
'Content-Type',
|
|
'Authorization',
|
|
'X-Requested-With',
|
|
'device-remember-token',
|
|
'Access-Control-Allow-Origin',
|
|
'Access-Control-Allow-Headers',
|
|
'Origin',
|
|
'Accept'
|
|
],
|
|
origin: function(origin, callback) {
|
|
if (whitelist.indexOf(origin) !== -1) {
|
|
callback(null, true)
|
|
} else {
|
|
callback(null, true)
|
|
//callback(new Error('Not allowed by CORS'))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
app.get('/run', [
|
|
//check('access_token').isLength({ min: 40 }),
|
|
//check('llo').isBase64()
|
|
],
|
|
cors(corsOptions), (req, res, next) => {
|
|
|
|
var RES = new Object();
|
|
RES.code = req.query["code"]
|
|
console.error('socket get from client' + RES.code);
|
|
|
|
RES.error = false
|
|
RES.error_msg = "ok"
|
|
|
|
io.emit("iotdata", RES)
|
|
io.in('iot').emit('message', RES);
|
|
res.json(RES)
|
|
|
|
});
|
|
|
|
app.post('/run', [
|
|
//check('access_token').isLength({ min: 40 }),
|
|
//check('llo').isBase64()
|
|
],
|
|
cors(corsOptions), (req, res, next) => {
|
|
console.error('socket post from client');
|
|
io.emit("customEmit", 'data')
|
|
|
|
var RES = new Object();
|
|
RES.error = false
|
|
RES.error_msg = "ok"
|
|
res.json(RES)
|
|
|
|
});
|
|
|
|
|
|
/*
|
|
io.on('connection', s => {
|
|
|
|
s.on("iot", function (room) {
|
|
s.join(room);
|
|
console.error('hi');
|
|
s.to(room).emit('message', 'hi from server');
|
|
io.emit('iotdata', 'hi from server');
|
|
});
|
|
|
|
});
|
|
*/
|
|
|
|
|
|
io.on('connection', s => {
|
|
console.error('socket connection');
|
|
var id = s.id
|
|
s.on('pingServerEmit', obj => {
|
|
console.error('socket.io pingServer');
|
|
var data = obj+' testserver'
|
|
io.emit("customEmit", data)
|
|
//io.broadcast.to('roomiot').emit('message', data)
|
|
io.in('iot').emit('message', data);
|
|
console.error('from client '+ s.id + ' obj ' + obj);
|
|
});
|
|
|
|
s.on('subscribe', function(room) {
|
|
console.log('joining room', room);
|
|
s.join(room);
|
|
})
|
|
|
|
io.on('unsubscribe', function(room) {
|
|
console.log('leaving room', room);
|
|
io.leave(room);
|
|
})
|
|
|
|
// when the client emits 'new message', this listens and executes
|
|
s.on('log', (data, room) => {
|
|
//console.log(util.inspect(data, false, null, true /* enable colors */))
|
|
//s.broadcast.to(room).emit('message', data)
|
|
s.to('iot').emit('message', data);
|
|
console.log('broadcast', data);
|
|
|
|
});
|
|
//s.emit('message', 'message from server');
|
|
|
|
//s.emit("customEmit", 'data')
|
|
|
|
|
|
});
|
|
|
|
|
|
http.listen(8084, () => console.error('listening on http://localhost:8084/'));
|
|
console.error('socket.io example');
|
|
|