2016-11-16 10:55:44 +01:00

54 lines
1.2 KiB
JavaScript

// server.js
// import express ()
var express = require('express'); // npm install --save express
var app = express();
// import node.js http
var server = require('http').Server(app);
// import socket.io
var io = require('socket.io')(server); // npm install --save socket.io
/* ----------------------------------
Server and Socket Configuration
--------------------------------------*/
// tell express to server our index.html file
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// configure socket.io
// (1) when there is a connection
io.on('connection', function(socket) {
console.log('got a connection');
//io.emit('message from robot', 'Hi! my name is Reihtuag!'); // greetings
// (2) configure the connected socket to receive custom messages ('message from human')
socket.on('message from human', function(msg) {
console.log('got a human message: ' + msg);
//io.emit('message from robot', msg);
});
socket.on('disconnet', function() {
console.log('got a disconnection');
});
});
/* -------------------
Start the server
----------------------*/
// listen to connection on port 8088 --> http://localhost:8088
server.listen(8088, function () {
console.log('listening on port: ' + 8088);
});