Skip to content

Commit d2b084f

Browse files
committed
move websocket initialization to app component
1 parent fbd7050 commit d2b084f

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

ui/App/App.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Header from './components/Header.jsx';
44
import Sidebar from './components/Sidebar.jsx';
55
import Footer from './components/Footer.jsx';
66
import HiddenSidebar from './components/HiddenSidebar.jsx';
7+
import Socket from '../socket.js';
78

89
class App extends React.Component {
910
constructor(props) {
@@ -13,6 +14,7 @@ class App extends React.Component {
1314
this.facServStatus = this.facServStatus.bind(this);
1415
this.getSaves = this.getSaves.bind(this);
1516
this.getStatus = this.getStatus.bind(this);
17+
this.connectWebSocket = this.connectWebSocket.bind(this);
1618
this.state = {
1719
serverRunning: "stopped",
1820
serverStatus: {},
@@ -32,6 +34,13 @@ class App extends React.Component {
3234
browserHistory.push("/login");
3335
}
3436
}, 1000);
37+
this.connectWebSocket();
38+
}
39+
40+
connectWebSocket() {
41+
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
42+
let ws = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
43+
let socket = this.socket = new Socket(ws);
3544
}
3645

3746
flashMessage(message) {
@@ -127,7 +136,8 @@ class App extends React.Component {
127136
getStatus: this.getStatus,
128137
saves: this.state.saves,
129138
getSaves: this.getSaves,
130-
username: this.state.username}
139+
username: this.state.username,
140+
socket: this.socket}
131141
)}
132142

133143
<Footer />

ui/App/components/ConsoleContent.jsx

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import React from 'react';
22
import {IndexLink} from 'react-router';
3-
import Socket from '../../socket.js';
43

54
class ConsoleContent extends React.Component {
65
constructor(props) {
76
super(props);
87
this.componentDidMount = this.componentDidMount.bind(this);
9-
this.connectWebsocket = this.connectWebsocket.bind(this);
10-
this.handleCommand = this.handleCommand.bind(this);
11-
this.onConnect = this.onConnect.bind(this);
128
this.handleInput = this.handleInput.bind(this);
139
this.clearInput = this.clearInput.bind(this);
1410
this.clearHistory = this.clearHistory.bind(this);
@@ -23,43 +19,23 @@ class ConsoleContent extends React.Component {
2319
}
2420

2521
componentDidMount() {
26-
this.connectWebsocket();
22+
this.props.socket.emit("log subscribe");
23+
this.setState({connected: true});
24+
25+
this.props.socket.on('log update', this.newLogLine.bind(this));
2726
}
2827

2928
componentDidUpdate() {
3029
var el = this.refs.output;
31-
console.log(this.refs);
3230
var container = document.getElementById("console-output");
33-
console.log(container)
3431
container.scrollTop = this.refs.output.scrollHeight;
3532
}
3633

37-
connectWebsocket() {
38-
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
39-
let ws = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
40-
let socket = this.socket = new Socket(ws);
41-
socket.on('connect', this.onConnect.bind(this));
42-
socket.on('log update', this.newLogLine.bind(this));
43-
}
44-
45-
handleCommand(command) {
46-
this.refs.console.log(command);
47-
this.refs.console.return();
48-
}
49-
50-
onConnect() {
51-
this.setState({connected: true});
52-
this.socket.emit("log subscribe");
53-
}
54-
5534
handleInput(e) {
5635
if (e.key === "Enter") {
5736
var input_text = this.refs.term.value;
58-
59-
this.socket.emit("command send", input_text);
60-
37+
this.props.socket.emit("command send", input_text);
6138
this.addHistory(this.state.prompt + " " + input_text);
62-
6339
this.clearInput();
6440
}
6541
}
@@ -69,7 +45,7 @@ class ConsoleContent extends React.Component {
6945
}
7046

7147
clearHistory() {
72-
ths.setState({ history: []});
48+
ths.setState({ history: [] });
7349
}
7450

7551
addHistory(output) {
@@ -127,4 +103,8 @@ class ConsoleContent extends React.Component {
127103
}
128104
}
129105

106+
ConsoleContent.propTypes = {
107+
socket: React.PropTypes.object.isRequired,
108+
}
109+
130110
export default ConsoleContent;

ui/App/components/Users/UserTable.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class UserTable extends React.Component {
7575
}
7676
}
7777

78-
UserTable.proptypes = {
78+
UserTable.propTypes = {
7979
users: React.PropTypes.array.isRequired,
8080
listUsers: React.PropTypes.func.isRequired,
8181
}

0 commit comments

Comments
 (0)