Establish socket events

This commit is contained in:
Mattias Erming 2014-06-29 03:07:38 +02:00
parent fd2011764a
commit abd9099018
6 changed files with 53 additions and 30 deletions

View File

@ -16,63 +16,61 @@ function Shout() {
"quit", "quit",
"users" "users"
].forEach(function(e) { ].forEach(function(e) {
socket.on(e, function() { client[e].call(client, socket);
client[e].call(client, socket);
});
}); });
} }
Shout.prototype.auth = function(socket) { Shout.prototype.auth = function(socket) {
socket.on("auth", function(json) { socket.on("auth", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.init = function(socket) { Shout.prototype.init = function(socket) {
socket.on("init", function(json) { socket.on("init", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.join = function(socket) { Shout.prototype.join = function(socket) {
socket.on("join", function(json) { socket.on("join", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.msg = function(socket) { Shout.prototype.msg = function(socket) {
socket.on("msg", function(json) { socket.on("msg", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.network = function(socket) { Shout.prototype.network = function(socket) {
socket.on("network", function(json) { socket.on("network", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.nick = function(socket) { Shout.prototype.nick = function(socket) {
socket.on("nick", function(json) { socket.on("nick", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.part = function(socket) { Shout.prototype.part = function(socket) {
socket.on("part", function(json) { socket.on("part", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.quit = function(socket) { Shout.prototype.quit = function(socket) {
socket.on("quit", function(json) { socket.on("quit", function(data) {
console.log(json); console.log(data);
}); });
}; };
Shout.prototype.users = function(socket) { Shout.prototype.users = function(socket) {
socket.on("quit", function(json) { socket.on("users", function(data) {
console.log(json); console.log(data);
}); });
}; };

View File

@ -1,3 +1,4 @@
module.exports = { module.exports = {
port: 9000 port: 9000,
public: true
}; };

View File

@ -8,9 +8,11 @@ Chan.Type = {
QUERY: "query" QUERY: "query"
}; };
var id = 0;
function Chan(attr) { function Chan(attr) {
_.merge(this, _.extend({ _.merge(this, _.extend({
id: global.id = ++global.id || 1, id: id++,
type: Chan.Type.CHANNEL, type: Chan.Type.CHANNEL,
name: "", name: "",
messages: [], messages: [],

View File

@ -1,9 +1,19 @@
var _ = require("lodash");
module.exports = Client; module.exports = Client;
var id = 0;
function Client(attr) { function Client(attr) {
_.merge(this, _.extend({ _.merge(this, _.extend({
name: "", id: id++,
sockets: null, networks: [],
networks: [] sockets: null
}, attr)); }, attr));
} }
Client.prototype.emit = function(event, data) {
if (this.sockets != null) {
this.sockets.in(this.id).emit(event, data);
}
};

View File

@ -2,9 +2,11 @@ var _ = require("lodash");
module.exports = Network; module.exports = Network;
var id = 0;
function Network(attr) { function Network(attr) {
_.merge(this, _.extend({ _.merge(this, _.extend({
id: global.id = ++global.id || 1, id: id++,
connected: false, connected: false,
slate: null, slate: null,
host: "", host: "",

View File

@ -1,4 +1,5 @@
var _ = require("lodash"); var _ = require("lodash");
var Client = require("./models/client");
var config = require("../config") || {}; var config = require("../config") || {};
var http = require("connect"); var http = require("connect");
var io = require("socket.io"); var io = require("socket.io");
@ -43,18 +44,27 @@ var events = [
module.exports = function() { module.exports = function() {
sockets = io(http().use(http.static("client")).listen(config.port || 9000)); sockets = io(http().use(http.static("client")).listen(config.port || 9000));
sockets.on("connection", function(socket) { 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) { if (!client) {
socket.emit("auth"); socket.emit("auth");
socket.on("auth", auth); socket.on("auth", auth);
} else { } 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; var socket = this;
}; };