Pregunta

This is the basic structure of my code:

var url = "https://goinstant.net/Something/SomethingElse";
goinstant.connect(url, function (err, connection, lobby) {
  var something = lobby.key("something");
  // and also some other stuff
});

And then later, in a different script document, but still on the same page:

var url = "https://goinstant.net/Something/SomethingElse";
goinstant.connect(url, function (err, connection, lobby) {
  var something = lobby.key("something");
  // and also some other stuff
});

The basic exact same deal. Except now Google Chrome is giving me these errors:

WebSocket connection to 'wss://goinstant.net/engine.io/?acct=Something&app=SomethingElse&jwt=UtAhSf3vmpLNcT0…6MDbfhLZN3G4jNqmMQQjlnl&EIO=2&transport=websocket&sid=9yQO6vVBI1syXlsxAAhJ' failed: WebSocket is closed before the connection is established. platform.min.js:3
Uncaught TypeError: Cannot call method 'key' of undefined 

How do I fix this? I remove the second bit of code, and everything works fine.

UPDATE: So now I'm using the approved answer's code:

var url = "https://goinstant.net/Something/SomethingElse";
var opts = {sessionId: "", guestId: ""};
goinstant.connect(url, opts, function (err, connection, lobby) {
  var something = lobby.key("something");
  // and also some other stuff
});

But for some reason, it is still not working. It is showing the same errors, and one more about some kind of Access-Control-Allow-Origin trying to go to GoInstant's servers via http.

ANOTHER UPDATE: I copied the answer's code, and then it seemed to work fine. But what I did was this:

I opened around 20 Google Chrome tabs, all with the same test page.

The first 5 worked, and the last 15 didn't, with the same websocket error as before, and also something about key not defined.

I know I'm starting to be a nuisance, but how do I fix this?

¿Fue útil?

Solución

Use the undocumented sessionId and guestId properties on the optionsObject parameter of `goinstant.connect':

var url = "https://goinstant.net/ACCOUNT/APPLICATION";
var opts = {sessionId: null, guestId: null};

var conn1, lobby1, conn2, lobby2;

goinstant.connect(url, opts, function(err, connection, lobby) {
  conn1 = connection;
  lobby1 = lobby;

  lobby.key('conn2Key').on('set', function(value, context) {
    console.log('-----------------------');
    console.log('Saw a set on conn2Key');
    console.log('User:', context.userId);
    console.log('Value:', value);
    console.log('This connection is user:', lobby.self().name.split('/')[2]);
    console.log('-----------------------');
  });
});

goinstant.connect(url, opts, function(err, connection, lobby) {
  conn2 = connection;
  lobby2 = lobby;

  setTimeout(function() {
    lobby.key('conn2Key').set('Hi!');
  }, 1000);
});

When set to an empty string or null, the connection will generate a new guest user and session for the connection.

By default, guest user information within a GoInstant application is shared across tabs, and sessions are reused within tabs (so if you reload, you can properly reconnect). This is why using two instances breaks; the same sessionId attempts to connect twice.

This feature is undocumented and unsupported, so it may change or break in the future.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top