Create socket connection and send a message
This commit is contained in:
parent
23d10df83a
commit
58de4bb47d
|
@ -21,6 +21,7 @@
|
|||
"dependencies": {
|
||||
"express": "^4.15.3",
|
||||
"react": "^15.6.1",
|
||||
"react-dom": "^15.6.1"
|
||||
"react-dom": "^15.6.1",
|
||||
"socket.io": "^2.0.3"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,12 @@ app.get('/hello', function (req, res) {
|
|||
res.send('Hello World!')
|
||||
})
|
||||
|
||||
app.listen(3000, function () {
|
||||
const server = app.listen(3000, function () {
|
||||
console.log('Example app listening on port 3000!')
|
||||
})
|
||||
|
||||
const io = require('socket.io').listen(server);
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
socket.emit('message', 'hello socket');
|
||||
});
|
||||
|
|
22
src/app.js
22
src/app.js
|
@ -1,4 +1,5 @@
|
|||
import React from 'react';
|
||||
import io from 'socket.io-client';
|
||||
|
||||
export default class App extends React.Component {
|
||||
constructor(props) {
|
||||
|
@ -8,9 +9,30 @@ export default class App extends React.Component {
|
|||
ioState: {
|
||||
dout: [false],
|
||||
},
|
||||
socket: io.connect()
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.connect();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.setState({socket: this.state.socket.removeAllListeners()});
|
||||
}
|
||||
|
||||
connect() {
|
||||
let socket = this.state.socket;
|
||||
|
||||
socket.on('connect', () => {
|
||||
console.log('connected');
|
||||
});
|
||||
|
||||
socket.on('message', (data) => {
|
||||
console.log(data);
|
||||
});
|
||||
}
|
||||
|
||||
doutSet(pin, value) {
|
||||
this.setState({
|
||||
ioState: {
|
||||
|
|
Loading…
Reference in New Issue
Block a user