Skip to content

Allow emit() to send a string on Android #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void initialize(String connection, ReadableMap options) {
* @param items The data to pass through the SocketIo engine to the server endpoint.
*/
@ReactMethod
public void emit(String event, ReadableMap items) {
public void emitData(String event, ReadableMap items) {
HashMap<String, Object> map = SocketIoReadableNativeMap.toHashMap((ReadableNativeMap) items);
if (mSocket != null) {
mSocket.emit(event, new JSONObject(map));
Expand All @@ -75,6 +75,21 @@ public void emit(String event, ReadableMap items) {
}
}

/**
* Emit event to server
* @param event The name of the event.
* @param item The string to pass through the SocketIo engine to the server endpoint.
*/
@ReactMethod
public void emitString(String event, String item) {
if (mSocket != null) {
mSocket.emit(event, item);
}
else {
Log.e(TAG, "Cannot execute emit. mSocket is null. Initialize socket first!!!");
}
}

/**
* Generates a Listener that handles an event. We've made it generic so that all response
* data will be packed into the items hash. Data is sent to the ReactNative JS layer.
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class Socket {
}

emit (event, data) {
this.sockets.emit(event, data);
if (typeof data === 'string') {
this.sockets.emitString(event, data);
} else {
this.sockets.emitData(event, data);
}
}

joinNamespace (namespace) {
Expand Down
10 changes: 9 additions & 1 deletion ios/RNSwiftSocketIO/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ class SocketIO: NSObject {
* Emit event to server
*/

@objc func emit(event: String, items: AnyObject) -> Void {
@objc func emitData(event: String, items: AnyObject) -> Void {
self.socket.emit(event, items)
}

/**
* Emit event to server
*/

@objc func emitString(event: String, items: AnyObject) -> Void {
self.socket.emit(event, items)
}

Expand Down
3 changes: 2 additions & 1 deletion ios/RNSwiftSocketIO/SocketBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ @interface RCT_EXTERN_MODULE(SocketIO, NSObject)
RCT_EXTERN_METHOD(connect)
RCT_EXTERN_METHOD(disconnect)
RCT_EXTERN_METHOD(reconnect)
RCT_EXTERN_METHOD(emit:(NSString*)event items:(id)items)
RCT_EXTERN_METHOD(emitString:(NSString*)event items:(id)items)
RCT_EXTERN_METHOD(emitData:(NSString*)event items:(id)items)
RCT_EXTERN_METHOD(joinNamespace:(NSString *)namespace)
RCT_EXTERN_METHOD(leaveNamespace)

Expand Down