Created
March 16, 2016 17:21
-
-
Save riza/312e9e7821fa62082409 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| express = require('express') | |
| app = express() | |
| http = require('http') | |
| connected = {} | |
| app.use express.static(__dirname + '/assets') | |
| app.get '/', (req, res) -> | |
| res.sendFile 'index.html', root: 'views' | |
| return | |
| app.get '/connected', (req, res) -> | |
| res.json connected | |
| return | |
| server = http.createServer(app).listen(3000, -> | |
| console.log '' | |
| console.log '============================================================' | |
| console.log '3TAG Started' | |
| console.log '============================================================' | |
| return | |
| ) | |
| io = require('socket.io').listen(server) | |
| io.on 'connection', (socket) -> | |
| socket.on 'search', (tags) -> | |
| `var tags` | |
| tags = tags.split(',') | |
| connected[socket.id] = [ | |
| tags[0] | |
| tags[1] | |
| tags[2] | |
| ] | |
| connected[socket.id].matched = false | |
| for clientId of connected | |
| if `clientId != socket.id` | |
| for tagKey of connected[clientId] | |
| connectedTag = connected[clientId][tagKey] | |
| tags.forEach (tag) -> | |
| if `connected[clientId].matched == false` | |
| if `connectedTag == tag` | |
| if io.sockets.connected[socket.id] | |
| io.sockets.connected[socket.id].emit 'matched', | |
| status: true | |
| match: clientId | |
| socket.join clientId | |
| return | |
| return | |
| socket.on 'typing', (data) -> | |
| if `data.typing == true` | |
| socket.broadcast.to(data.room).emit 'typing', 1 | |
| else | |
| socket.broadcast.to(data.room).emit 'typing', 0 | |
| return | |
| socket.on 'send message', (data) -> | |
| connected[socket.id].matched = true | |
| socket.broadcast.to(data.room).emit 'send to stranger', message: data.message | |
| return | |
| socket.on 'subscribe', (room) -> | |
| console.log '# 2 client matched and joined to room -> ', room | |
| socket.join room | |
| socket.broadcast.to(room).emit 'joined', | |
| status: 1 | |
| socket: socket.id | |
| return | |
| socket.on 'disconnect', -> | |
| socket.broadcast.to(socket.id).emit 'user disconnected', 1 | |
| delete connected[socket.id] | |
| return | |
| return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment