Skip to content

websocket: include reception timestamp in all events. #28

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Libraries/WebSocket/WebSocket.js
Original file line number Diff line number Diff line change
@@ -239,15 +239,15 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
data = BlobManager.createFromOptions(ev.data);
break;
}
this.dispatchEvent(new WebSocketEvent('message', {data}));
this.dispatchEvent(new WebSocketEvent('message', {data, timestamp: ev.timestamp}));
}),
this._eventEmitter.addListener('websocketOpen', ev => {
if (ev.id !== this._socketId) {
return;
}
this.readyState = this.OPEN;
this.protocol = ev.protocol;
this.dispatchEvent(new WebSocketEvent('open'));
this.dispatchEvent(new WebSocketEvent('open', {timestamp: ev.timestamp}));
}),
this._eventEmitter.addListener('websocketClosed', ev => {
if (ev.id !== this._socketId) {
@@ -258,6 +258,7 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
new WebSocketEvent('close', {
code: ev.code,
reason: ev.reason,
timestamp: ev.timestamp,
}),
);
this._unregisterEvents();
@@ -271,11 +272,13 @@ class WebSocket extends (EventTarget(...WEBSOCKET_EVENTS): any) {
this.dispatchEvent(
new WebSocketEvent('error', {
message: ev.message,
timestamp: ev.timestamp,
}),
);
this.dispatchEvent(
new WebSocketEvent('close', {
message: ev.message,
timestamp: ev.timestamp,
}),
);
this._unregisterEvents();
2 changes: 2 additions & 0 deletions React/CoreModules/RCTWebSocketModule.h
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)sendData:(NSData *)data forSocketID:(nonnull NSNumber *)socketID;

- (void)sendEventWithName:(NSString *)name body:(NSDictionary *)body;

// Blocking call that waits until there are no more remaining actions on the queue
- (void)flush;

16 changes: 16 additions & 0 deletions React/CoreModules/RCTWebSocketModule.mm
Original file line number Diff line number Diff line change
@@ -16,6 +16,13 @@

#import "CoreModulesPlugins.h"

(NSNumber)currentJsDate
{
double seconds = [NSDate timeIntervalSinceReferenceDate] + NSTimeIntervalSince1970;

return [NSNumber numberWithLong: seconds * 1000];
}

@implementation RCTSRWebSocket (React)

- (NSNumber *)reactTag
@@ -202,6 +209,15 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket
return std::make_shared<facebook::react::NativeWebSocketModuleSpecJSI>(params);
}

- (void)sendEventWithName:(NSString *)name
body:(NSDictionary *)body
{
NSMutableDictionary *mutableBody = [body mutableCopy];
mutableBody["timestamp"] = currentJsDate();

[super sendEventWithName:name body:mutableBody]
}

@end

@implementation RCTBridge (RCTWebSocketModule)
Original file line number Diff line number Diff line change
@@ -69,6 +69,8 @@ private void sendEvent(String eventName, WritableMap params) {
ReactApplicationContext reactApplicationContext = getReactApplicationContextIfActiveOrWarn();

if (reactApplicationContext != null) {
params.putDouble("timestamp", System.currentTimeMillis());

reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);