RejectedSoftware Forums

Sign up

Reload problem with WebSocket

I'm trying to exchange data between websockets, following webchat tutorial and a old post.

The route that handles web socket connections is:

void getWS(scope WebSocket socket) {
      logInfo("Web.getWS \t\tstart getWS");
      auto t = runTask({
         while (socket.connected) {
            //....
            socket.send(json);
            //....
         }
      });

      while (socket.waitForData) {
          auto message = socket.receiveText();
         if (message.length) {
            logInfo("Web.waitForData \t\t%s", message);
         } else {
            logInfo("no data");
         }
      }
      logInfo("Web.getWS \t\tdisconnected.");
}

It works, but if I reload the page (with ctrl-R in Firefox) the program exits with message:

Program exited with code -11

Here in github the complete project.

Where am'I wrong?

Thanks for your help.

Re: Reload problem with WebSocket

On Wed, 13 Jan 2016 13:10:31 GMT, Orfeo wrote:

I'm trying to exchange data between websockets, following webchat tutorial and a old post.

The route that handles web socket connections is:

void getWS(scope WebSocket socket) {
      logInfo("Web.getWS \t\tstart getWS");
      auto t = runTask({
         while (socket.connected) {
            //....
            socket.send(json);
            //....
         }
      });

      while (socket.waitForData) {
          auto message = socket.receiveText();
         if (message.length) {
            logInfo("Web.waitForData \t\t%s", message);
         } else {
            logInfo("no data");
         }
      }
      logInfo("Web.getWS \t\tdisconnected.");
}

It works, but if I reload the page (with ctrl-R in Firefox) the program exits with message:

Program exited with code -11

Here in github the complete project.

Where am'I wrong?

Thanks for your help.

The problem is that inside of the writer task, depending on the order of events, the WebSocket object might get accessed after getWS has already returned (which is when the object gets destroyed). To fix this, add t.join(); right before the end of getWS to wait for the task to finish first.

The tutorial had the same issue and I've corrected that, too.

Re: Reload problem with WebSocket

On Wed, 13 Jan 2016 18:12:02 GMT, Sönke Ludwig wrote:

On Wed, 13 Jan 2016 13:10:31 GMT, Orfeo wrote:

I'm trying to exchange data between websockets, following webchat tutorial and a old post.

The route that handles web socket connections is:

void getWS(scope WebSocket socket) {
      logInfo("Web.getWS \t\tstart getWS");
      auto t = runTask({
         while (socket.connected) {
            //....
            socket.send(json);
            //....
         }
      });

      while (socket.waitForData) {
          auto message = socket.receiveText();
         if (message.length) {
            logInfo("Web.waitForData \t\t%s", message);
         } else {
            logInfo("no data");
         }
      }
      logInfo("Web.getWS \t\tdisconnected.");
}

It works, but if I reload the page (with ctrl-R in Firefox) the program exits with message:

Program exited with code -11

Here in github the complete project.

Where am'I wrong?

Thanks for your help.

The problem is that inside of the writer task, depending on the order of events, the WebSocket object might get accessed after getWS has already returned (which is when the object gets destroyed). To fix this, add t.join(); right before the end of getWS to wait for the task to finish first.

The tutorial had the same issue and I've corrected that, too.

It works like a charm....
Thanks you very much for your help!