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",
"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);
});
};

View file

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

View file

@ -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: [],

View file

@ -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);
}
};

View file

@ -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: "",

View file

@ -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;
};