Created
June 15, 2020 23:54
-
-
Save williaanlopes/245b4b063b887ed25aa191d3693ff9a3 to your computer and use it in GitHub Desktop.
Exemplo de socket.io com multiplos namespaces e rooms entre clientes
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script> | |
<script type="text/javascript"> | |
var socket = io.connect('http://localhost:3000/api/v1', { | |
query: new URLSearchParams({token: 'asdad', room: '123'}).toString() | |
}); | |
socket.on('connected', function(data) { | |
console.log(data); | |
}); | |
socket.on('message', function(data) { | |
console.log('Incoming message:', data); | |
}); | |
</script> | |
</body> | |
</html> |
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
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
io.use(function(socket, next) { | |
// make sure the handshake data looks good as before | |
// if error do this: | |
// next(new Error('not authorized')); | |
// else just call next | |
next(); | |
}); | |
const userNamespace = io.of('/api/v1'); | |
const adminNamespace = io.of('/api/v1/admin'); | |
userNamespace.on('connection', function(socket) { | |
var privateRoom = socket.handshake.query.token; | |
var publicRoom = socket.handshake.query.room; | |
socket.join(privateRoom); | |
socket.join(publicRoom); | |
socket.emit('connected', { | |
message: 'success connected', | |
status: 'OK', | |
id: socket.id.split('#')[1], | |
current_room: privateRoom, | |
private_room: publicRoom | |
}); | |
socket.on('join room', function(room) { | |
socket.join(room.name); | |
socket.emit('connected', { | |
message: 'success joined room', | |
status: 'OK' | |
}); | |
}); | |
socket.on('leave room', function(room) { | |
socket.leave(room.name); | |
socket.emit('leaving room', { | |
message: 'success leaving room', | |
status: 'OK' | |
}); | |
}); | |
socket.on('disconnect', function() { | |
console.log('Client disconnected.'); | |
}); | |
}); | |
adminNamespace.on('connection', function(socket) { | |
socket.on('join room', function(room) { | |
socket.join(room.name); | |
socket.emit('connected', { | |
message: 'success joined room', | |
status: 'OK' | |
}); | |
}); | |
socket.on('leave room', function(room) { | |
socket.leave(room.name); | |
socket.emit('leaving room', { | |
message: 'success leaving room', | |
status: 'OK' | |
}); | |
}); | |
socket.on('disconnect', function() { | |
console.log('Client disconnected.'); | |
}); | |
}); | |
app.get('/', function(req, res){ | |
res.send('server is running'); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment