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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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 createRandomNickname = len => { | |
var text = ''; | |
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
for(var i = 0; i < len; i++) { | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
return text; | |
}; | |
ReactDOM.render(<Main nickname={createRandomNickname(6)}/>, document.getElementById('container')); |
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 Main = React.createClass({ | |
getInitialState() { | |
return { | |
users: [], | |
messages: [] | |
} | |
}, | |
componentDidMount() { | |
//will show later | |
}, |
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 Main = React.createClass({ | |
getInitialState() { | |
//already shown | |
}, | |
componentDidMount() { | |
var socket = io(); | |
var props = this.props; | |
var users = this.state.users; | |
var messages = this.state.messages; |
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 io = require('socket.io')(server); | |
var sourceConnect = Rx.Observable.create(function(observer) { | |
io.on('connection', function(socket) { | |
socket.emit('my socketId', {'socketId': socket.id, 'connectTime': Date.now()}); | |
socket.on('client connect', function(data) { | |
observer.onNext({'socket': socket, 'data': data, 'event': 'client connect'}); | |
}); | |
}); |
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 sourceDisconnect = Rx.Observable.create(function(observer) { | |
io.on('connection', function(socket) { | |
socket.on('disconnect', function(data) { | |
observer.onNext({'socketId': socket.id, 'event': 'client disconnect'}); | |
}); | |
}); | |
}); | |
var observerDisconnect = sourceDisconnect | |
.subscribe(function(obj) { |
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
app.post('/message', function(req, res) { | |
io.emit('message', req.body); | |
}); |
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 AppBar = React.createClass({ | |
render() { | |
return ( | |
<div className="navbar-fixed"> | |
<nav> | |
<div className="nav-wrapper"> | |
<a href="#" className="brand-logo center">RxJS ReactJS SocketIO MaterializeCSS</a> | |
<ul id="nav-mobile" className="left hide-on-med-and-down"> | |
<li><a href="#">About</a></li> | |
</ul> |
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 PresencePane = React.createClass({ | |
render() { | |
return ( | |
<div> | |
<h4>Active Users</h4> | |
<table className="striped"> | |
<thead> | |
<tr> | |
<th data-field="id">Nickname</th> | |
<th data-field="name">Time joined</th> |
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 ChatPane = React.createClass({ | |
componentDidMount() { | |
var button = document.getElementById('sendBtn'); | |
var textField = document.getElementById('message-input'); | |
var clickStream = Rx.Observable.fromEvent(button, 'click').map(e => true); | |
var enterKeyPressedStream = Rx.Observable.fromEvent(textField, 'keyup').filter(e => e.keyCode == 13); | |
var textEnteredStream = Rx.Observable.fromEvent(textField, 'keyup').map(e => e.target.value); | |
var sendMessageStream = Rx.Observable.merge(clickStream, enterKeyPressedStream); | |
OlderNewer