Quobis / QoffeeSIP

QoffeeSIP is a complete Javascript SIP stack that can be used in a website to exploit all the multimedia capabilities of WebRTC technology. Instead of using pure Javascript, QoffeeSIP has been coded with CoffeeScript so you can easily modify it to suit your needs.
GNU Lesser General Public License v3.0
27 stars 5 forks source link

Trying to register automatically with the webserver if webserver connection is lost #14

Open sumantasen opened 10 years ago

sumantasen commented 10 years ago

Hi All,

I am trying to implementation some sort of automatic reregistration when the websocket is closed.

.

When websocket is getting closed, we are getting callback on onclose() in qoffeesip.js

Now we are triggering one event in webphone.js. There we are able to create the user object again and trying to register

However, the next event is not getting triggered if the webserver is till down.

Here is what I have done in webphone.js

UI.prototype.renderWebsocketClosed = function(message) { console.log("WebSocket is closed now, try re-register");

 //Register again.
 var line, onlyAudio, onopen, server, serverRE, sipServer, stunServer, turnServer, _ref1,
      _this = this;

            window.autoanswering = $("#auto-answer").is(":checked");

    $("#pass-reg").val("123");
    $("#server-reg").val("ws://x.x.x.x:5082");
    $("#only-audio").attr("checked", false);
    $("#stun-server").val("");
    $("#stun-server").val("");
    $("#turn-server-credential").val("");

    User.create({
      user: $("#user-reg").val(),
      password: $("#pass-reg").val(),
      sipServer: $("#server-reg").val(),
      audioSession: $("#only-audio").is(":checked"),
      stunServer: $("#stun-server").val(),
      turnServer: $("#stun-server").val(),
      turnCredential: $("#turn-server-credential").val()
    });

    _ref1 = $("#user-reg").val().split("@"), this.register.ext = _ref1[0], this.register.domain = _ref1[1];
    this.register.pass = $("#pass-reg").val() || this.register.ext;
    server = $("#server-reg").val();
    onlyAudio = $("#only-audio").is(":checked");
    stunServer = {
      url: "stun:" + $("#stun-server").val()
    };
    turnServer = {
      url: "turn:" + $("#turn-server").val(),
      credential: $("#turn-server-credential").val()
    };
    if (stunServer.url === "stun:") {
      stunServer = {
        //"url": "stun:204.236.188.164" //stun.1.google.com
        "url": "stun:173.194.76.127:19302" //stun.1.google.com

      };
    }
    serverRE = /(wss?):\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\:(\d{2,5}))?((\/\w+)*)/;
    line = serverRE.exec(server);
    sipServer = {};
    if (line != null) {
      sipServer.transport = line[1];
      sipServer.ip = line[2];
      sipServer.port = line[4];
      sipServer.path = line[5] || "";
    } else {
      sipServer.ip = "x.x.x.x";
      sipServer.port = "5082";
      sipServer.path = "";
      sipServer.transport = "ws";
    }
    onopen = function() {
      _this.api.on("new-state", _this.newState);
      _this.api.on("instant-message", _this.renderInstantMessage);
      _this.api.on("websocket-closed", _this.renderWebsocketClosed);
      _this.api.register(_this.register.ext, _this.register.pass, _this.register.domain);
      _this.$registerButton.addClass("disabled");
      return _this.$registerButton.addClass("disabled");
    };
    this.api = new API({
      server: sipServer,
      turnServer: turnServer,
      stunServer: stunServer,
      mediaElements: this.mediaElements,
      onopen: onopen,
      mediaConstraints: {
        audio: true,
        video: !onlyAudio
      }
    });

    return this.api.on("localstream", function() {
      return _this.$mediaLocal.removeClass("hidden");
    });
  };

Any idea why this websocket closed call back is not hitting the second time.

damianfral commented 10 years ago

Hi @sumantasen, I think this behaviour is normal.

  1. You get a websocket opened, so you are able to register.
  2. Your server gets down, you catch an "websocket-closed".
  3. You try to open a websocket again, but the server is still down, so you cannot get a websocket-closed cause this new websocket never gets opened.
sumantasen commented 10 years ago

I have also tries to incorporate onerror(), we still get the same error.