rdh-socket-client 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/package.json +1 -1
- package/src/Channel.ts +28 -0
- package/src/index.ts +3 -14
package/README.md
CHANGED
|
@@ -18,13 +18,16 @@ const client = new rdhClientSocket({
|
|
|
18
18
|
app_key: 'APP_key'
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
+
const client_first_channel = client.channel('channelName');
|
|
22
|
+
|
|
21
23
|
// Subscribe to a channel and listen for an event
|
|
22
|
-
|
|
24
|
+
client_first_channel.on('eventName', (data) => {
|
|
23
25
|
console.log(data.message);
|
|
24
26
|
});
|
|
25
27
|
|
|
26
28
|
// Emit a custom event with data
|
|
27
|
-
|
|
29
|
+
client_first_channel.emit('myEvent', mydata);
|
|
30
|
+
|
|
28
31
|
```
|
|
29
32
|
eplace `APP_ID` and `APP_key` with your actual application ID and key. Make sure to customize the channel name, event name, and data according to your application's requirements.
|
|
30
33
|
|
package/package.json
CHANGED
package/src/Channel.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Socket } from 'socket.io-client';
|
|
2
|
+
|
|
3
|
+
export default class Channel {
|
|
4
|
+
private channel: string;
|
|
5
|
+
private socket: Socket;
|
|
6
|
+
|
|
7
|
+
constructor(channel: string, socket: Socket) {
|
|
8
|
+
this.channel = channel;
|
|
9
|
+
this.socket = socket;
|
|
10
|
+
this.init();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
init() {
|
|
14
|
+
this.socket.emit('subscribe', {
|
|
15
|
+
channel: this.channel
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
on(event_name: string, callback: (...args: any[]) => void) {
|
|
20
|
+
const real_event = this.channel+"."+event_name;
|
|
21
|
+
this.socket.on(real_event, callback);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
emit(event_name: string, data: any[]) {
|
|
25
|
+
// this.socket.on(event_name, callback);
|
|
26
|
+
this.socket.emit(event_name, { data: data, _channel_name: this.channel})
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import Channel from "./Channel";
|
|
1
2
|
import { Options } from "./types";
|
|
2
3
|
import io, { Socket } from 'socket.io-client';
|
|
3
4
|
|
|
@@ -20,20 +21,8 @@ export default class RdhClientSocket {
|
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
channel(channel_name: string):
|
|
24
|
+
channel(channel_name: string): Channel {
|
|
24
25
|
let finalChannelName = this.options.app_id+'_'+channel_name;
|
|
25
|
-
this.socket
|
|
26
|
-
channel: finalChannelName
|
|
27
|
-
})
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
on(event_name: string, callback: (...args: any[]) => void) {
|
|
32
|
-
this.socket.on(event_name, callback);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
emit(event_name: string, data: any[]) {
|
|
36
|
-
// this.socket.on(event_name, callback);
|
|
37
|
-
this.socket.emit(event_name, data)
|
|
26
|
+
return new Channel(finalChannelName, this.socket);
|
|
38
27
|
}
|
|
39
28
|
}
|