This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
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.
- Loading branch information
0 parents
commit 044f262
Showing
13 changed files
with
7,590 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
db | ||
raft-log |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2018 Benjamin Boudreau <boudreau.benjamin@gmail.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the 'Software'), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# socket.io-raft | ||
|
||
Adapter to enable broadcasting of events to multiple separate socket.io server nodes without third-party dependencies. | ||
|
||
## Raft | ||
|
||
This adapter leverages the [Raft](https://ramcloud.stanford.edu/raft.pdf) consensus algorithm to broadcast messages around. |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Setup basic express server | ||
var express = require('express'); | ||
var app = express(); | ||
var server = require('http').createServer(app); | ||
var io = require('socket.io')(server); | ||
|
||
var raftAdapter = require('../../.'); | ||
var port = process.env.PORT || 3000; | ||
var serverName = process.env.NAME || 'Unknown ('+port +')'; | ||
|
||
const raftPort = process.env.RAFT_PORT; | ||
const raftPeers = (process.env.RAFT_PEERS || "").split(','); | ||
io.adapter(raftAdapter({ port: raftPort, peers: raftPeers })); | ||
|
||
server.listen(port, function () { | ||
console.log('Server listening at port %d', port); | ||
console.log('Hello, I\'m %s, how can I help?', serverName); | ||
}); | ||
|
||
// Routing | ||
app.use(express.static(__dirname + '/public')); | ||
|
||
// Chatroom | ||
|
||
var numUsers = 0; | ||
|
||
io.on('connection', function (socket) { | ||
socket.emit('my-name-is', serverName); | ||
|
||
var addedUser = false; | ||
|
||
// when the client emits 'new message', this listens and executes | ||
socket.on('new message', function (data) { | ||
// we tell the client to execute 'new message' | ||
socket.broadcast.emit('new message', { | ||
username: socket.username, | ||
message: data | ||
}); | ||
}); | ||
|
||
// when the client emits 'add user', this listens and executes | ||
socket.on('add user', function (username) { | ||
if (addedUser) return; | ||
|
||
// we store the username in the socket session for this client | ||
socket.username = username; | ||
++numUsers; | ||
addedUser = true; | ||
socket.emit('login', { | ||
numUsers: numUsers | ||
}); | ||
// echo globally (all clients) that a person has connected | ||
socket.broadcast.emit('user joined', { | ||
username: socket.username, | ||
numUsers: numUsers | ||
}); | ||
}); | ||
|
||
// when the client emits 'typing', we broadcast it to others | ||
socket.on('typing', function () { | ||
socket.broadcast.emit('typing', { | ||
username: socket.username | ||
}); | ||
}); | ||
|
||
// when the client emits 'stop typing', we broadcast it to others | ||
socket.on('stop typing', function () { | ||
socket.broadcast.emit('stop typing', { | ||
username: socket.username | ||
}); | ||
}); | ||
|
||
// when the user disconnects.. perform this | ||
socket.on('disconnect', function () { | ||
if (addedUser) { | ||
--numUsers; | ||
|
||
// echo globally that this client has left | ||
socket.broadcast.emit('user left', { | ||
username: socket.username, | ||
numUsers: numUsers | ||
}); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.