zeus
4 years ago
6 changed files with 156 additions and 1 deletions
@ -0,0 +1 @@ |
|||
Course examples |
@ -0,0 +1,31 @@ |
|||
|
|||
const app = require('express')(); |
|||
const http = require('http').Server(app); |
|||
var path = require('path'); |
|||
|
|||
options = { |
|||
secure:true, |
|||
reconnect: true, |
|||
rejectUnauthorized : false |
|||
}; |
|||
|
|||
|
|||
var io2 = require('socket.io-client'); |
|||
var socket = io2.connect('http://localhost:8084', options); |
|||
|
|||
var msg2 = "c= 120"; |
|||
socket.emit('log', msg2); |
|||
|
|||
/* |
|||
var io = require('socket.io')(http); |
|||
|
|||
app.get('/log', (req, res) => { |
|||
socket.emit('log', 'send from get'); |
|||
res.send('<h1>send</h1>'); |
|||
}); |
|||
|
|||
http.listen(8085, () => { |
|||
console.log('listening on *:8085'); |
|||
}); |
|||
*/ |
|||
|
@ -0,0 +1,90 @@ |
|||
const app = require('express')(); |
|||
const http = require('http').Server(app); |
|||
var path = require('path'); |
|||
var io = require('socket.io')(http); |
|||
const MongoClient = require('mongodb').MongoClient; |
|||
|
|||
|
|||
app.get('/', (req, res) => { |
|||
res.send('<h1>Hello world!</h1>'); |
|||
}); |
|||
|
|||
app.get('/test', (req, res) => { |
|||
|
|||
|
|||
var user="test1" |
|||
var pass="newpass" |
|||
//mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/$MONGO_INITDB_DATABASE” -u $MONGO_INITDB_USERNAME mongo “mongodb://localhost:30001,localhost:30002,localhost:30003/app_swarmlab” -u app_swarmlab
|
|||
//mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs
|
|||
//var mongourl = "mongodb://"+user+":"+pass+"@localhost:30001,localhost:30002,localhost:30003/app_swarmlab?replicaSet=rs0&authSource=admin"
|
|||
/* |
|||
use admin |
|||
db.createUser( |
|||
{ |
|||
user: "test1", |
|||
pwd: 'newpass', // Or "<cleartext password>"
|
|||
roles: [ { role: "readWrite", db: "app_swarmlab" } ], |
|||
authenticationRestrictions: [ { |
|||
clientSource: ["192.168.1.7"], |
|||
serverAddress: ["192.168.80.2", "192.168.80.3", "192.168.80.4"] |
|||
} ] |
|||
} |
|||
) |
|||
*/ |
|||
|
|||
var mongourl = "mongodb://"+user+":"+pass+"@192.168.80.2:27017/app_swarmlab?replicaSet=rs0&authSource=admin" |
|||
const OPTS = { |
|||
useNewUrlParser: true, |
|||
useUnifiedTopology: true |
|||
}; |
|||
var options = { |
|||
useNewUrlParser: true, |
|||
useUnifiedTopology: true |
|||
} |
|||
|
|||
|
|||
MongoClient.connect(mongourl, options, function(err, client){ |
|||
if(err){ |
|||
console.log(err); |
|||
} else { |
|||
console.log(JSON.stringify('mongo ----------------connected')) |
|||
const db = client.db('app_swarmlab'); |
|||
db.collection('logs').find({}).toArray() |
|||
.then(item => { |
|||
console.log('item '+JSON.stringify(item)) |
|||
for (let i in item) { |
|||
console.log(JSON.stringify('items' + item[i])) |
|||
} |
|||
}) |
|||
|
|||
} |
|||
}) |
|||
res.send('<h1>test!</h1>'); |
|||
|
|||
}); |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
io.on('connection', s => { |
|||
console.error('socket connection'); |
|||
|
|||
s.on('log', (data, room) => { |
|||
s.to('iot').emit('message', data); |
|||
console.log('broadcast', data); |
|||
|
|||
}); |
|||
//s.emit('message', 'message from server');
|
|||
|
|||
|
|||
|
|||
}); |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
http.listen(8084, () => { |
|||
console.log('listening on *:8084'); |
|||
}); |
@ -0,0 +1,24 @@ |
|||
var path = require('path'); |
|||
var app = require('express')(); |
|||
var http = require('http').Server(app); |
|||
var io = require('socket.io')(http); |
|||
|
|||
|
|||
|
|||
io.on('connection', s => { |
|||
console.error('socket connection'); |
|||
|
|||
s.on('log', (data, room) => { |
|||
s.to('iot').emit('message', data); |
|||
console.log('broadcast', data); |
|||
|
|||
}); |
|||
//s.emit('message', 'message from server');
|
|||
|
|||
|
|||
|
|||
}); |
|||
|
|||
|
|||
http.listen(8084, () => console.error('listening on http://localhost:8084/')); |
|||
console.error('socket.io example'); |
@ -0,0 +1,8 @@ |
|||
{ |
|||
"dependencies": { |
|||
"express": "^4.17.1", |
|||
"mongodb": "^3.6.5", |
|||
"socket.io": "^4.0.0", |
|||
"socket.io-client": "^4.0.0" |
|||
} |
|||
} |
Loading…
Reference in new issue