-
-
Save khle/ef1b252ec8dca3294bce to your computer and use it in GitHub Desktop.
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; | |
var self = this; | |
var socketIdStream = Rx.Observable.create(observer => { | |
socket.on('my socketId', data => { observer.onNext(data); }); | |
}); | |
socketIdStream.subscribe(data => { | |
socket.emit('client connect', { | |
nickname: props.nickname, | |
socketId: data.socketId, | |
connectTime: data.connectTime | |
}); | |
}); | |
var socketAllUsersStream = Rx.Observable.create(observer => { | |
socket.on('all users', data => { observer.onNext(data); }); | |
}); | |
socketAllUsersStream.subscribe(data => { | |
self.setState({users: data}); | |
}); | |
var socketMessageStream = Rx.Observable.create(observer => { | |
socket.on('message', data => { observer.onNext(data); }); | |
}); | |
socketMessageStream.subscribe(data => { | |
messages.push(data); | |
self.setState(messages); //data is {'message': text, 'who': nickname, 'timestamp': Date.now} | |
}); | |
}, | |
render() { | |
//already shown | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment