question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reconnect with same PWD

See original GitHub issue

I am using xterm.js in my project. Every time I refresh my page or reconnect socket when a socket connection is broken due to internet fluctuation from the client. The current PWD directory is lost and it falls to specified CWD directory which is user home in my case. So again I have to do cd where I was working.

How can I connect and remain at same PWD where I was last time before page refreshing or socket disconnect?

One of the things I tried is to store term object and connect through the same object when reconnecting if it is already present. Not deleting process and object in on WebSocket disconnect.

var http = require('http');
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
var pty = require('node-pty');
var cors = require('cors');
app.use(cors());
app.options('*', cors());
var terminals = {}; //global terminals

function getUser(token) {
    return new Promise((resolve, reject) => {
      try {
        return http.get({
            host: '',
            path: '',
            headers: {'token': token}
        }, function(response) {
            // Continuously update stream with data
            var body = '';
            response.on('data', function(d) {
                body += d;
            });
            response.on('end', function() {
                return resolve(JSON.parse(body));
            });
        });
      } catch (err) {
        console.log('Api failed');
        console.log(err);
        reject;
      }
    })
}

app.ws('/terminals/:user_id', function (ws, req) {
  try {
    getUser(req.params.user_id) /* cheking with api if user exist in my database*/
      .then(user_info => {
        if(terminals[parseInt(req.params.user_id)]){
          var term = terminals[parseInt(req.params.user_id)];
        }else {
          var term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], {
            name: 'xterm-color',
            cwd: cwd,
            env: process.env
          });
          terminals[parseInt(req.params.user_id)] = term;
        }

        term.on('data', function(data) {
          ws.send(data);
        });

        ws.on('message', function(msg) {
          term.write(msg);
        });

        ws.on('close', function () {
          // process.kill(term.pid);
          // delete terminals[parseInt(req.params.pid)];
          // delete logs[req.params.pid];
        });
      })
      .catch(err => {
      })
  } catch (err) {
      console.log('Terminal webSocket failed');
      console.log(err);
  }
});

app.listen(3000);

This is not working for me. This gets me connect only first time but when I refresh my page terminal does not connect with existing store object.

Also, this has a problem if the spawned process is killed by the system but it still remains in javascript object and script try to reconnect with same term object it will fail.

Update:: I noticed that on event in not getting bind again to new term object which was saved.

Any guidelines how to achieve reconnect with same PWD.

Details

  • OS version: Mac OS
  • xterm.js version: 2.2.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
PradeepJaiswarcommented, Jun 5, 2017

If anyone facing the same issue. This can be also solved by just updating the ~/.bashrc

Putting below two line in ~/.bashrc file worked for me

PROMPT_COMMAND+='printf %s "$PWD" > ~/.storepwd'
[ -s ~/.lastdirectory ] && cd `cat ~/.lastdirectory`

Ref https://stackoverflow.com/questions/767040/save-last-working-directory-on-bash-logout

0reactions
PradeepJaiswarcommented, Jun 3, 2017

@Tyriar thanks for reply but it did not worked 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

xterm.js reconnect with same PWD - Stack Overflow
Every time I refresh my page or reconnect socket when a socket connection is broken due to internet fluctuation from the client. The...
Read more >
How to reconnect to a disconnected ssh session - Server Fault
When you first log in, run screen. You get another shell, run commands in that. If you're disconnected, the screen process keeps the...
Read more >
If I get a new router and make the SSID and password ... - Quora
If I get a new router and make the SSID and password the same as the old one, will devices still be able...
Read more >
linux - Spawn new terminal window with the same directory as ...
Edit --> Preferences --> General --> Open new terminals in: --> select tab. So new terminals will open in the same window with...
Read more >
Bug? Reconnect is possible without having stored the ...
I have many accounts on same database, a few have no passwords stored and few do. Each time I connect to an account...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found