Last active
November 7, 2017 21:21
-
-
Save AlexChittock/dd27b0a162ee61df2cd7ba7352835af2 to your computer and use it in GitHub Desktop.
Simple server distributes 'action' messages between all connected clients.
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
import SocketIO from 'socket.io' | |
const io = SocketIO(3030) | |
const history = [] | |
io.on('connection', (socket) => { | |
// Replay actions on new clients | |
socket.on('init', (_) => history.forEach(action => socket.emit('action', action))) | |
socket.on('action', (action) => { | |
// Persist actions in memory | |
history.push(action) | |
socket.broadcast.emit('action', action) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment