From d0b4cd4f7e6364a35a420f2f3b0fcdbb502d8a9a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 5 Aug 2015 00:51:32 -0400 Subject: basic routing --- server/ws.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 server/ws.js (limited to 'server/ws.js') diff --git a/server/ws.js b/server/ws.js new file mode 100644 index 0000000..4715d34 --- /dev/null +++ b/server/ws.js @@ -0,0 +1,37 @@ + +var server = require('socket.io') + +var ws = module.exports = {} + +ws.listen = function(app){ + ws.io = server(app) +} +ws.add = function(name){ + var room = {} + room.users = {} + + var ns = base.io.of('/' + name) + + ns.on('connection', function(socket){ + + var username + + socket.on('join', function(data){ + username = data.username + if (room.users[username]) { + // already connected? + room.users[username].disconnect() + room.users[username] = socket + } + }) + + socket.on('message', function(data){ + ns.emit('message', data) + }) + + socket.on('disconnect', function(){ + delete room.users[username] + }) + + }) +} -- cgit v1.2.3-70-g09d2 From 3b7da491072e5c366738ff549e8958d97948076a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 5 Aug 2015 10:45:17 -0400 Subject: hook into ws --- public/js/lib/video.js | 2 +- public/js/lib/views/lobby/index.js | 2 +- public/js/vendor/util.js | 2 ++ server/index.js | 6 +++++- server/ws.js | 3 +++ 5 files changed, 12 insertions(+), 3 deletions(-) (limited to 'server/ws.js') diff --git a/public/js/lib/video.js b/public/js/lib/video.js index 45e6043..2eb5c82 100644 --- a/public/js/lib/video.js +++ b/public/js/lib/video.js @@ -2,7 +2,7 @@ var video = (function(){ var video = {} var mx - video.init = function(opt){ + video.init = function(media){ video.build() } diff --git a/public/js/lib/views/lobby/index.js b/public/js/lib/views/lobby/index.js index 70b63c4..0306445 100644 --- a/public/js/lib/views/lobby/index.js +++ b/public/js/lib/views/lobby/index.js @@ -9,7 +9,7 @@ var LobbyView = View.extend({ }, join: function(){ - var name = this.$createRoom.sanitize() + var name = this.$createRoom.sanitizeName() if (! name) { return } window.location.href = "/v/" + name } diff --git a/public/js/vendor/util.js b/public/js/vendor/util.js index 7c73ae2..73a25ad 100644 --- a/public/js/vendor/util.js +++ b/public/js/vendor/util.js @@ -5,12 +5,14 @@ if (window.$) { $.fn.enable = function() { return $(this).attr("disabled",null) } $.fn.disable = function() { return $(this).attr("disabled","disabled") } $.fn.sanitize = function(s) { return trim(sanitize($(this).val())) } + $.fn.sanitizeName = function(s) { return trim(sanitizeName($(this).val())) } $.fn.htmlSafe = function(s) { return $(this).html(sanitize(s)) } $.fn.toDollars = function(i) { return $(this).html((i/100).toFixed(2)) } } function trim (s){ return s.replace(/^\s+/,"").replace(/\s+$/,"") } function sanitize (s){ return (s || "").replace(new RegExp("[<>&]", 'g'), "") } +function sanitizeName (s){ return (s || "").replace(new RegExp("[^-_a-zA-Z0-9]", 'g'), "") } function stripHTML (s){ return (s || "").replace(/<[^>]+>/g, "") } function capitalize (s){ return s.split(" ").map(capitalizeWord).join(" ") } function capitalizeWord (s){ return s.charAt(0).toUpperCase() + s.slice(1) } diff --git a/server/index.js b/server/index.js index 8b8c738..d08cf2e 100644 --- a/server/index.js +++ b/server/index.js @@ -35,10 +35,14 @@ app.use(express.query()) app.get("/", function(req,res){ res.render("pages/lobby", {}) }) -app.post("/v/", function(req,res){ +app.post("/v/:room", function(req,res){ + var room = util.sanitizeName(req.params.room) + ws.add(room) res.render("pages/room") }) app.get("/v/:room", function(req,res){ + var room = util.sanitizeName(req.params.room) + ws.add(room) res.render("pages/room") }) diff --git a/server/ws.js b/server/ws.js index 4715d34..860319a 100644 --- a/server/ws.js +++ b/server/ws.js @@ -3,10 +3,13 @@ var server = require('socket.io') var ws = module.exports = {} +var rooms = {} + ws.listen = function(app){ ws.io = server(app) } ws.add = function(name){ + if (name in rooms) { return } var room = {} room.users = {} -- cgit v1.2.3-70-g09d2