From abd909901818071a94f6c864365cd55c0cf5e46e Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 29 Jun 2014 03:07:38 +0200 Subject: [PATCH] Establish socket events --- client/js/shout.js | 40 +++++++++++++++++++--------------------- config.js | 3 ++- lib/models/chan.js | 4 +++- lib/models/client.js | 16 +++++++++++++--- lib/models/network.js | 4 +++- lib/shout.js | 16 +++++++++++++--- 6 files changed, 53 insertions(+), 30 deletions(-) diff --git a/client/js/shout.js b/client/js/shout.js index 5ab09da3..0eccba81 100644 --- a/client/js/shout.js +++ b/client/js/shout.js @@ -16,63 +16,61 @@ function Shout() { "quit", "users" ].forEach(function(e) { - socket.on(e, function() { - client[e].call(client, socket); - }); + client[e].call(client, socket); }); } Shout.prototype.auth = function(socket) { - socket.on("auth", function(json) { - console.log(json); + socket.on("auth", function(data) { + console.log(data); }); }; Shout.prototype.init = function(socket) { - socket.on("init", function(json) { - console.log(json); + socket.on("init", function(data) { + console.log(data); }); }; Shout.prototype.join = function(socket) { - socket.on("join", function(json) { - console.log(json); + socket.on("join", function(data) { + console.log(data); }); }; Shout.prototype.msg = function(socket) { - socket.on("msg", function(json) { - console.log(json); + socket.on("msg", function(data) { + console.log(data); }); }; Shout.prototype.network = function(socket) { - socket.on("network", function(json) { - console.log(json); + socket.on("network", function(data) { + console.log(data); }); }; Shout.prototype.nick = function(socket) { - socket.on("nick", function(json) { - console.log(json); + socket.on("nick", function(data) { + console.log(data); }); }; Shout.prototype.part = function(socket) { - socket.on("part", function(json) { - console.log(json); + socket.on("part", function(data) { + console.log(data); }); }; Shout.prototype.quit = function(socket) { - socket.on("quit", function(json) { - console.log(json); + socket.on("quit", function(data) { + console.log(data); }); }; Shout.prototype.users = function(socket) { - socket.on("quit", function(json) { - console.log(json); + socket.on("users", function(data) { + console.log(data); }); }; diff --git a/config.js b/config.js index 9e833e0f..0b735705 100644 --- a/config.js +++ b/config.js @@ -1,3 +1,4 @@ module.exports = { - port: 9000 + port: 9000, + public: true }; diff --git a/lib/models/chan.js b/lib/models/chan.js index 9284e19a..bca66921 100644 --- a/lib/models/chan.js +++ b/lib/models/chan.js @@ -8,9 +8,11 @@ Chan.Type = { QUERY: "query" }; +var id = 0; + function Chan(attr) { _.merge(this, _.extend({ - id: global.id = ++global.id || 1, + id: id++, type: Chan.Type.CHANNEL, name: "", messages: [], diff --git a/lib/models/client.js b/lib/models/client.js index 958fe8e0..342cf2f8 100644 --- a/lib/models/client.js +++ b/lib/models/client.js @@ -1,9 +1,19 @@ +var _ = require("lodash"); + module.exports = Client; +var id = 0; + function Client(attr) { _.merge(this, _.extend({ - name: "", - sockets: null, - networks: [] + id: id++, + networks: [], + sockets: null }, attr)); } + +Client.prototype.emit = function(event, data) { + if (this.sockets != null) { + this.sockets.in(this.id).emit(event, data); + } +}; \ No newline at end of file diff --git a/lib/models/network.js b/lib/models/network.js index 1ae5a597..d8a3cc69 100644 --- a/lib/models/network.js +++ b/lib/models/network.js @@ -2,9 +2,11 @@ var _ = require("lodash"); module.exports = Network; +var id = 0; + function Network(attr) { _.merge(this, _.extend({ - id: global.id = ++global.id || 1, + id: id++, connected: false, slate: null, host: "", diff --git a/lib/shout.js b/lib/shout.js index 15b7202c..984c6f1c 100644 --- a/lib/shout.js +++ b/lib/shout.js @@ -1,4 +1,5 @@ var _ = require("lodash"); +var Client = require("./models/client"); var config = require("../config") || {}; var http = require("connect"); var io = require("socket.io"); @@ -43,18 +44,27 @@ var events = [ module.exports = function() { sockets = io(http().use(http.static("client")).listen(config.port || 9000)); sockets.on("connection", function(socket) { - init(socket); + if (config.public) { + init(socket, new Client()); + } else { + init(socket); + } }); }; -var init = function(socket, client) { +function init(socket, client) { if (!client) { socket.emit("auth"); socket.on("auth", auth); } else { + socket.on("input", function(data) { input(client, data); }); + socket.join(client.id); + socket.emit("init", { + networks: client.networks + }); } }; -var auth = function() { +function auth() { var socket = this; };