11import React from 'react' ;
22import { IndexLink } from 'react-router' ;
3- import Socket from '../../socket.js' ;
43
54class 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+
130110export default ConsoleContent ;
0 commit comments