-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mudando estrategia: usando lib socket.io pra fazer a magica
- Loading branch information
Lucas Chuairi Cruz
committed
Aug 23, 2018
1 parent
606e0d9
commit aafff1e
Showing
6 changed files
with
111 additions
and
152 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,21 @@ | ||
"use strict"; | ||
var app = require('express')(); | ||
var http = require('http').Server(app); | ||
var io = require('socket.io')(http); | ||
|
||
var webSocketsServerPort = 3001; | ||
var webSocketServer = require('websocket').server; | ||
var http = require('http'); | ||
io.on('connection', function(socket) { | ||
console.log('a user connected'); | ||
|
||
var history = [ ]; | ||
var clients = [ ]; | ||
|
||
function htmlEntities(str) { | ||
return String(str) | ||
.replace(/&/g, '&').replace(/</g, '<') | ||
.replace(/>/g, '>').replace(/"/g, '"'); | ||
} | ||
|
||
var server = http.createServer(function(request, response) { | ||
// Not important for us. We're writing WebSocket server, | ||
// not HTTP server | ||
}); | ||
socket.on('disconnect', function() { | ||
console.log('user disconnected'); | ||
}); | ||
|
||
server.listen(webSocketsServerPort, function() { | ||
console.log((new Date()) + " Server is listening on port " | ||
+ webSocketsServerPort); | ||
}); | ||
socket.on('message', function(msg) { | ||
console.log('message: ' + msg); | ||
io.emit('message', msg); | ||
}); | ||
|
||
var wsServer = new webSocketServer({ | ||
httpServer: server | ||
}); | ||
|
||
wsServer.on('request', function(request) { | ||
console.log((new Date()) + ' Connection from origin ' + request.origin + '.'); | ||
|
||
var connection = request.accept(null, request.origin); | ||
var index = clients.push(connection) - 1; | ||
var userName = false; | ||
|
||
console.log((new Date()) + ' Connection accepted.'); | ||
// send back chat history | ||
if (history.length > 0) { | ||
connection.sendUTF( | ||
JSON.stringify({ type: 'history', data: history} )); | ||
} | ||
// user sent some message | ||
connection.on('message', function(message) { | ||
// accept only text | ||
if (message.type === 'utf8') { | ||
|
||
// first message sent by user is their name | ||
if (userName === false) { | ||
userName = htmlEntities(message.utf8Data); | ||
console.log((new Date()) + ' User is known as: ' + userName); | ||
} else { // log and broadcast the message | ||
console.log((new Date()) + ' Received Message from ' | ||
+ userName + ': ' + message.utf8Data); | ||
|
||
// we want to keep history of all sent messages | ||
var obj = { | ||
time: (new Date()).getTime(), | ||
text: htmlEntities(message.utf8Data), | ||
author: userName, | ||
}; | ||
history.push(obj); | ||
history = history.slice(-100); | ||
// broadcast message to all connected clients | ||
var json = JSON.stringify({ type:'message', data: obj }); | ||
for (var i=0; i < clients.length; i++) { | ||
clients[i].sendUTF(json); | ||
} | ||
} | ||
} | ||
}); | ||
// user disconnected | ||
connection.on('close', function(connection) { | ||
if (userName !== false) { | ||
console.log((new Date()) + " Peer " | ||
+ connection.remoteAddress + " disconnected."); | ||
// remove user from the list of connected clients | ||
clients.splice(index, 1); | ||
} | ||
}); | ||
http.listen(3001, function() { | ||
console.log('listening on *:3001'); | ||
}); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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