From 2f63919b30583b440727b250c2d0503b24f3d666 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 5 Aug 2015 12:49:47 -0400 Subject: socket stuff frm yesterday --- public/js/lib/socket.js | 59 ---------------------------------------------- server/index.js | 28 +++++++++++++++------- server/websocket.js | 34 ++++++++++++++++++++++++++ views/partials/scripts.ejs | 4 +++- 4 files changed, 57 insertions(+), 68 deletions(-) delete mode 100644 public/js/lib/socket.js create mode 100644 server/websocket.js diff --git a/public/js/lib/socket.js b/public/js/lib/socket.js deleted file mode 100644 index 4fd11a5..0000000 --- a/public/js/lib/socket.js +++ /dev/null @@ -1,59 +0,0 @@ -var socket = function(){ - var socket = {} - socket.connect: function () { - if (this.socket) return; - var socketPath = window.location.origin + '/' + posthang.room.subdomain - - this.socket = io(socketPath); - // this.socket.on('connect', function(){ console.log(new Date(), "connected")}); - this.socket.on('ready', $.proxy(this.socketReady, this)); - this.socket.on('error', $.proxy(this.socketError, this)); - this.socket.on('connect', $.proxy(this.socketConnected, this)); - this.socket.on('reconnect', $.proxy(this.socketReconnected, this)); - this.socket.on('disconnect', $.proxy(this.disconnected, this)) - - this.socket.on('users', $.proxy(this.updateUsers, this)); - this.socket.on('message', $.proxy(this.chatView.append, this.chatView)); - this.socket.on('info', $.proxy(this.chatView.appendInfo, this.chatView)); - this.socket.on('mediaId', $.proxy(this.mediaView.updateMediaId, this.mediaView)); - this.socket.on('messageId', $.proxy(this.chatView.updateMessageId, this.chatView)); - this.socket.on('roomDeleted', $.proxy(this.roomDeleted, this)); - this.socket.on('userBanned', $.proxy(this.userBanned, this)); - this.socket.on('fave', $.proxy(this.faveView.faved, this.faveView)) - - $(window).on("scroll", $.proxy(this.didScroll, this)) - $(window).on("focus", $.proxy(this.focus, this)) - $(window).on("blur", $.proxy(this.blur, this)) - - this.chatView.fetchMessages(); - this.mediaView.fetchMedia(); - }, - - socketReady: function (obj) { - console.log(new Date(), "ready") - - // presumably we might have reconnected? - if (this.socketIsReady) { - this.chatView.fetchAndDedupe() - } - else { - this.socketIsReady = true - this.checkIfLoaded() - } - }, - - socketError: function (a,b,c){ - console.log(new Date(), "error", a, b, c) - }, - socketConnected: function (){ - console.log(new Date(), "connected") - }, - socketReconnected: function (){ - console.log(new Date(), "reconnected") - }, - - socketDisconnected: function (){ - this.chatView.appendInfo({ content: "Disconnected." }) - } - } -} \ No newline at end of file diff --git a/server/index.js b/server/index.js index 89ba350..64965b5 100644 --- a/server/index.js +++ b/server/index.js @@ -11,8 +11,10 @@ var http = require('http'), multer = require('multer'), path = require('path'); +var websocket = require('./websocket') + var app = express() -var server +var server, io app.set('port', config.port) app.set('views', path.join(__dirname, '../views')) @@ -24,21 +26,31 @@ app.use(require('cookie-parser')()) // app.use(require('body-parser')()) // app.use(require('multer')) app.use(express.query()) -// app.use(require('method-override')()) // app.set('trust proxy', true) // if (config.env.development) { // app.use(require('express-error-handler')) // } +server = http.createServer(app) +server.listen(app.get('port'), function () { + console.log('asdf-yt server listening on port ' + app.get('port')); +}) + +var io = websocket.init(server) + +var rooms = {} + +app.all('*', function(req, res, next){ + res.locals.config = config + next() +}) app.get("/", function(req,res){ res.render("pages/index", {}) }) app.get("/v/:room", function(req,res){ + var room = req.params.room + if (! (room in rooms)) { + rooms[room] = websocket.bind(room) + } res.render("pages/room") }) - -server = http.createServer(app) -server.listen(app.get('port'), function () { - console.log('asdf-yt server listening on port ' + app.get('port')); -}) - diff --git a/server/websocket.js b/server/websocket.js new file mode 100644 index 0000000..ca54b68 --- /dev/null +++ b/server/websocket.js @@ -0,0 +1,34 @@ +var websocket = module.exports = {} +var io + +websocket.init = function(server){ + io = require('socket.io')(server) + return io +} + +websocket.bind = function(room){ + var room_socket = io.of('/' + room) + + var users = {}, messages = [] + + room_socket.on('connection', function(socket){ + + var nick + + socket.on("join", function(data){ + nick = data.nick + users[data.nick] = true + }) + + socket.on("msg", function(data){ + if (messages.length > 20) { messages.shift() } + messages.push(data) + room_socket.emit("msg", data) + }) + + socket.on("disconnect", function(){ + delete users[nick] + }) + + }) +} diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 0e3bc02..95192b9 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -1,10 +1,12 @@ + + - + -- cgit v1.2.3-70-g09d2