summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2015-08-05 12:49:47 -0400
committerJules Laplace <jules@okfoc.us>2015-08-05 12:49:47 -0400
commit2f63919b30583b440727b250c2d0503b24f3d666 (patch)
tree95430856f827aa0db70902b405fa67d5290d0b2d
parent0b47720bad6a9c3b3447da97a89b91a1f8ab7d71 (diff)
socket stuff frm yesterday
-rw-r--r--public/js/lib/socket.js59
-rw-r--r--server/index.js28
-rw-r--r--server/websocket.js34
-rw-r--r--views/partials/scripts.ejs4
4 files changed, 57 insertions, 68 deletions
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 @@
+<script src="//[[- config.host ]]/socket.io/socket.io.js"></script>
<script src="js/vendor/zepto.min.js"></script>
<script src="js/lib/view/view.js"></script>
<script src="js/lib/view/formview.js"></script>
<script src="js/lib/view/router.js"></script>
<script src="js/lib/parser.js"></script>
+<script src="js/lib/ws.js"></script>
<script src="js/lib/user.js"></script>
-<script src="js/site/index.js"></script>
+<script src="js/lib/chat.js"></script>
<script src="js/index.js"></script>
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>