Skip to content
Snippets Groups Projects
Commit c51cfb89 authored by Ronnie Friman's avatar Ronnie Friman
Browse files

added socket listener

parent 6d963478
No related branches found
No related tags found
2 merge requests!31Development,!27Socket update
...@@ -3,6 +3,7 @@ import "../node_modules/leaflet-draw/dist/leaflet.draw.css"; ...@@ -3,6 +3,7 @@ import "../node_modules/leaflet-draw/dist/leaflet.draw.css";
import "./App.css"; import "./App.css";
import UserMap from "./components/UserMap"; import UserMap from "./components/UserMap";
import Header from "./components/Header"; import Header from "./components/Header";
import ClientSocket from "./components/Socket";
class App extends Component { class App extends Component {
constructor() { constructor() {
...@@ -57,6 +58,10 @@ class App extends Component { ...@@ -57,6 +58,10 @@ class App extends Component {
handleLayerChange={this.handleLayerChange} handleLayerChange={this.handleLayerChange}
handleGameChange={this.handleGameChange} handleGameChange={this.handleGameChange}
/> />
,
{this.state.currentGameId && (
<ClientSocket gameId={this.state.currentGameId} />
)}
</div> </div>
); );
} }
......
import React from "react";
import io from "socket.io-client";
const socketUrl = process.env.REACT_APP_API_URL;
export default class ClientSocket extends React.Component {
constructor(props) {
super(props);
this.state = {
// stores the socket object for notifications
sock: null,
// stores updates sent by socket
update: null
};
}
// iniate the socket on component mount
componentWillMount() {
console.log("iniated socket");
this.initSocket();
}
// disconnect the socket on component dismount
componentWillUnmount() {
this.state.sock.disconnect();
}
initSocket = () => {
const socket = io(socketUrl);
// set the socket to listen gameId-thread
socket.on(this.props.gameId, data => {
// check socket update type
this.setState({ update: data });
console.log(this.state.update);
});
this.setState({ sock: socket });
};
render() {
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment