Skip to content

Commit

Permalink
Added a network module to replace the socket io module
Browse files Browse the repository at this point in the history
  • Loading branch information
dalewebster48 committed May 24, 2017
1 parent 6687287 commit 9c90757
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Layouts/Layout.json
@@ -0,0 +1,3 @@
{
"name":"Layout"
}
39 changes: 24 additions & 15 deletions lib/ever-layout-bridge.js
@@ -1,3 +1,4 @@
require("rootpath")
var http = require('http');
var fs = require("fs");
var url = require("url");
Expand Down Expand Up @@ -44,22 +45,25 @@ if (fs.existsSync(imageDirectory) == false) {
}

// Create sockets
var socket = require("socket.io")(server);
socket.on("connection" , function(client){
console.log("Connected");
// var socket = require("socket.io")(server);
// socket.on("connection" , function(client){
// console.log("Connected");

client.emit("connection");
// client.emit("connection");

client.on("disconnect" , function(){
console.log("Disconnected");
});
// client.on("disconnect" , function(){
// console.log("Disconnected");
// });

client.on("report" , function(data){
if (message = data.message) {
console.log(message);
}
})
});
// client.on("report" , function(data){
// if (message = data.message) {
// console.log(message);
// }
// })
// });

var network = require("../lib/network");
network.createServer(port);

// Watch the layouts directory for changes
watch(layoutDirectory , {recursive:true} , function(event , filename){
Expand All @@ -73,12 +77,17 @@ watch(layoutDirectory , {recursive:true} , function(event , filename){
if (err) return console.log("There was an error");
console.log("Serving " + realName);
// Parse the JSON from the file
socket.emit("layout-update" , {
// socket.emit("layout-update" , {
// "layoutName": realName,
// "layout": data
// });

network.sendData({
"layoutName": realName,
"layout": data
});
});
});

server.listen(port);
// server.listen(port);
console.log("Listening on port " + port);
56 changes: 56 additions & 0 deletions lib/network.js
@@ -0,0 +1,56 @@
// Load the TCP Library
var net = require('net');

var network = {
clients : [],
createServer : function (port) {
net.createServer(function(socket){
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort

// Put this new client in the list
network.clients.push(socket);

console.log("Connected to: " + socket.name);

// Handle incoming messages from clients.
// socket.on('data', function (data) {
// broadcast(socket.name + "> " + data, socket);
// });

// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
console.log(socket.name + " disconnected");
});
} , port);
},
sendData : function (data) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
}
};

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {

// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}

}).listen(3000);

module.exports = network;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"minimist": "^1.2.0",
"node-watch": "^0.5.1",
"rootpath": "^0.1.2",
"socket.io": "^1.7.2"
}
}

0 comments on commit 9c90757

Please sign in to comment.