Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket .ping and .onpong missing #14855

Closed
msageryd opened this issue Jul 6, 2017 · 4 comments
Closed

Websocket .ping and .onpong missing #14855

msageryd opened this issue Jul 6, 2017 · 4 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@msageryd
Copy link

msageryd commented Jul 6, 2017

Websocket in RN seems to to respond correctly with a pong when ping is received (as per the Websockets spec.).

It would be great if there also where a function to send a ping and an event for knowing when pong is received.

Like this:

var ws = new WebSocket('ws://host.com/path');

ws.ping();

ws.onpong = () => {
  // pong received  
  // this socket is alive
};
@msageryd
Copy link
Author

msageryd commented Jul 6, 2017

Update.
I found that ping() is already implemented, but not documented. Couldn't find any pongevent though.

@hramos hramos closed this as completed Jul 6, 2017
@tapz
Copy link

tapz commented Jan 15, 2018

Android native code uses OkHttp for WebSocket and that does not seem to handle pong. However, the RN codebase includes an iOS version of OkHttp and it supports pong. RCTSRWebSocket handles pong and calls the delegate, but RCTWebSocketModule does not implement didReceivePong method. Just a few lines of code copy-pasted from onopen to relay the event to JavaScript...

RCTWebSocketModule.m:

- (NSArray *)supportedEvents
{
  return @[@"websocketMessage",
           @"websocketOpen",
           @"websocketFailed",
           @"websocketClosed", 
           @"websocketPong"];
}

- (void)webSocket:(RCTSRWebSocket *)webSocket didReceivePong
{
  [self sendEventWithName:@"websocketPong" body:@{
    @"id": webSocket.reactTag
  }];
}

WebSocket.js:

this._eventEmitter.addListener('websocketPong', ev => {
  if (ev.id !== this._socketId) {
    return;
  }
  this.dispatchEvent(new WebSocketEvent('pong'));
}),

RCTReconnectingWebSocket.m
...
RCTWebSocketExecutor.m
...

@msageryd
Copy link
Author

That's great news! Thank you.
Maybe a PR on this?

@tapz
Copy link

tapz commented Jan 15, 2018

I'm thinking about that :-) Probably need to do it to Android OkHttp too.

@facebook facebook locked as resolved and limited conversation to collaborators Jul 6, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants