Created
August 17, 2018 20:38
-
-
Save davidsaccavino/6f4c4ecdfa024371a9a657bb440a64e0 to your computer and use it in GitHub Desktop.
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
/* | |
this is the server.js file | |
*/ | |
io.on('connection', (client) => { | |
client.on('helloUser', () => { | |
console.log('sending'); | |
client.emit('hello', 'hello world') | |
}); | |
}); | |
const port = 8000; | |
io.listen(port); | |
console.log('listening on port ', port); | |
/* | |
this is the main app.js file | |
*/ | |
import React, { Component } from 'react'; | |
import openSocket from 'socket.io-client'; | |
const socket = openSocket('http://localhost:8000'); | |
class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
hello: "not hello" | |
}; | |
socket.emit('helloUser'); | |
socket.on('hello', msg => this.setState({hello: msg})); | |
} | |
render() { | |
return ( | |
<div> | |
<p> | |
{this.state.hello} | |
</p> | |
</div> | |
); | |
} | |
} | |
export default App; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment