Refactored and added ensl client

This commit is contained in:
Chris Blanchard 2015-07-22 01:00:05 +01:00
parent 5c2fa4f34a
commit e293e1b5ca
7 changed files with 156 additions and 30 deletions

View file

@ -2,24 +2,27 @@
var fs = require("fs");
var path = require("path");
var enslClient = require(path.join(__dirname, "../lib/ensl/client"))();
var chatController = require(path.join(__dirname, "../lib/chat/controller"));
module.exports = function (io) {
var root = io.of("/");
var authorised = io.of("/authorised");
var id = 2131;
// Authorisation
root.use(function (socket, next) {
socket._user = {
id: Math.floor(Math.random () * 10000000),
username: "Chris (" + socket.id.slice(0,5) + ")",
steamId: "11111111",
email: "cablanchard@gmail.com",
bans: [],
avatar: "http://www.ensl.org/local/avatars/6359.jpg"
};
enslClient.getUserById({
id: id
}, function (error, response, body) {
if (error) return next(error);
socket._user = body;
socket._user.avatar = enslClient.getFullAvatarUri(socket._user.avatar);
next();
});
});
var refreshGatherers = function (socket) {
var receiver = (socket !== undefined) ? socket : root;

27
lib/ensl/client.js Normal file
View file

@ -0,0 +1,27 @@
"use strict";
var request = require("request");
var env = process.env.NODE_ENV || "development";
function EnslClient (options) {
if (!(this instanceof EnslClient)) {
return new EnslClient(options);
}
this.baseUrl = (env === "production") ? "http://www.ensl.org/" : "http://staging.ensl.org";
}
EnslClient.prototype.getUserById = function (options, callback) {
var id = options.id;
var url = this.baseUrl + "/api/v1/users/" + id;
request({
url: url,
json: true
}, callback);
};
EnslClient.prototype.getFullAvatarUri = function (url) {
return this.baseUrl + url;
}
module.exports = EnslClient;

View file

@ -1,3 +1,7 @@
$(function () {
"use strict";
var GatherCounter = React.createClass({
render: function () {
return (
@ -11,6 +15,32 @@ var GatherCounter = React.createClass({
}
});
var UserLogin = React.createClass({
handleSubmit: function () {
},
render: function () {
return (
<form onSubmit={this.handleSubmit} >
<div className="input-group">
<input
id="btn-input"
type="text"
className="form-control"
placeholder="Choose an ID..." />
<span className="input-group-btn">
<input
type="submit"
className="btn btn-primary"
id="btn-chat"
value="Login" />
</span>
</div>
</form>
);
}
})
var Gatherer = React.createClass({
render: function () {
return (
@ -39,6 +69,9 @@ var GathererMenu = React.createClass({
});
return (
<ul className="nav" id="side-menu">
<li>
<UserLogin />
</li>
<GatherCounter {...this.props} />
{gatherers}
</ul>
@ -186,5 +219,27 @@ var MessageBar = React.createClass({
}
});
React.render(<GathererMenu count={0} gatherers={[]} />, document.getElementById('side-menu'));
React.render(<Chatroom history={[]}/>, document.getElementById('chatroom'));
var socket;
function initialiseComponents () {
socket = io("http://localhost:8000/")
.on("connect", function () {
console.log("Connected");
})
.on("reconnect", function () {
console.log("Reconnected");
})
.on("disconnect", function () {
console.log("Disconnected")
});
React.render(<GathererMenu count={0} gatherers={[]} />, document.getElementById('side-menu'));
React.render(<Chatroom history={[]}/>, document.getElementById('chatroom'));
};
initialiseComponents();
});

View file

@ -10,7 +10,7 @@
"scripts": {
"test": "NODE_ENV=test mocha spec/",
"start": "node index.js",
"compile:dev:react": "node_modules/react-tools/bin/jsx --watch --source-map-inline -x jsx lib/react/ public/js/",
"compile:dev:react": "node_modules/react-tools/bin/jsx --watch --no-cache-dir --source-map-inline -x jsx lib/react/ public/js/",
"compile:react": "node_modules/react-tools/bin/jsx -x jsx lib/react/ public/js/",
"dev": "nodemon index.js"
},
@ -30,7 +30,7 @@
"morgan": "~1.6.1",
"node-mysql": "~0.4.2",
"react-tools": "~0.13.3",
"request": "^2.60.0",
"request": "~2.60.0",
"socket.io": "~1.3.5",
"winston": "~1.0.1"
},

File diff suppressed because one or more lines are too long

View file

@ -1,13 +0,0 @@
var socket = io("http://localhost:8000/");
socket.on("connect", function () {
console.log("Connected!");
});
// socket.on("reconnect", function () {
// });
// socket.on("disconnect", function () {
// });

View file

@ -1,2 +1 @@
<script src="/js/client.js"></script>
<script src="/js/app.js"></script>