Skip to content

Commit

Permalink
socket 添加 namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
xughv committed Apr 2, 2017
1 parent 2927794 commit 96aaec3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 86 deletions.
7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ server.listen({
});

// socket.io connection
const connection = require('./server/shell').connection;
io.on('connection', connection);
const shellConn = require('./server/shell').connection;
io.of('/shell').on('connection', shellConn);

const fileConn = require('./server/file').connection;
io.of('/file').on('connection', fileConn);
32 changes: 32 additions & 0 deletions server/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require('path');
const ssh = require('ssh2').Client;
const TextDecoder = require('text-encoding').TextDecoder;
const config = require('read-config')(path.resolve(__dirname, '../config.json'));

exports.connection = function(socket) {
var conn = new ssh();

// ssh 连接相关配置
config.ssh = socket.request.session.ssh;

conn.on('banner', function(d) {
//need to convert to cr/lf for proper formatting
d = d.replace(/\r?\n/g, '\r\n');
socket.emit('data', d.toString('binary'));
}).on('ready', function() {

// TODO

}).on('end', function() {
socket.disconnect();
}).on('close', function() {
socket.disconnect();
}).on('error', function(err) {
console.error('Error: ' + err);
}).connect({
host: config.ssh.host,
port: config.ssh.port,
username: config.ssh.username,
password: config.ssh.password
});
}
82 changes: 0 additions & 82 deletions src/components/qterminal/index.js

This file was deleted.

File renamed without changes.
71 changes: 71 additions & 0 deletions src/components/term/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';

import './index.css';

export default class Term extends React.Component {

componentDidMount() {
this.init();
}

init = () => {
const terminalContainer = document.getElementById('terminal-container');
const term = new Terminal({ cursorBlink: true });
var socket;

term.open(terminalContainer);
term.fit();

if (document.location.pathname) {
var parts = document.location.pathname.split('/'),
base = parts.slice(0, parts.length - 1).join('/') + '/',
resource = base.substring(1) + 'socket.io';
socket = io.connect('/shell', { resource: resource });
} else {
socket = io.connect('/shell');
}

socket.on('connect', function() {

socket.emit('size', term.cols, term.rows);

term.on('data', function(data) {
socket.emit('data', data);
});

socket.on('title', function(data) {
document.title = data;
}).on('status', function(data) {
document.getElementById('status').innerHTML = data;
}).on('footer', function(data) {
document.getElementById('footer').innerHTML = data;
}).on('statusBackground', function(data) {
document.getElementById('status').style.backgroundColor = data;
}).on('data', function(data) {
term.write(data);
}).on('disconnect', function(err) {
document.getElementById('status').style.backgroundColor = 'red';
document.getElementById('status').innerHTML = 'WEBSOCKET SERVER DISCONNECTED' + err;
socket.io.reconnection(false);
}).on('error', function(err) {
document.getElementById('status').style.backgroundColor = 'red';
document.getElementById('status').innerHTML = 'ERROR ' + err;
});

});

}

render() {
return (
<div className="box">
<div id="header"></div>
<div id="terminal-container" className="terminal"></div>
<div id="bottomdiv">
<div id="status"></div>
<div id="footer"></div>
</div>
</div>
);
}
};
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import './main.css';

import FileExplorer from './components/file-explorer';
import Tabbar from './components/tabbar';
import QTerminal from './components/qterminal';
import Term from './components/term';

export default class App extends React.Component {

Expand Down Expand Up @@ -84,7 +84,7 @@ export default class App extends React.Component {
closeFile={this.closeFile} />
</Content>
<Footer>
<QTerminal />
<Term />
</Footer>
</Layout>
</Layout>
Expand Down

0 comments on commit 96aaec3

Please sign in to comment.