-
Notifications
You must be signed in to change notification settings - Fork 175
/
pusher-connector.ts
138 lines (119 loc) · 3.54 KB
/
pusher-connector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Connector } from './connector';
import {
PusherChannel,
PusherPrivateChannel,
PusherEncryptedPrivateChannel,
PusherPresenceChannel,
PresenceChannel,
} from './../channel';
/**
* This class creates a connector to Pusher.
*/
export class PusherConnector extends Connector {
/**
* The Pusher instance.
*/
pusher: any;
/**
* All of the subscribed channel names.
*/
channels: any = {};
/**
* Create a fresh Pusher connection.
*/
connect(): void {
if (typeof this.options.client !== 'undefined') {
this.pusher = this.options.client;
} else if (this.options.Pusher) {
this.pusher = new this.options.Pusher(this.options.key, this.options);
} else {
this.pusher = new Pusher(this.options.key, this.options);
}
}
/**
* Sign in the user via Pusher user authentication (https://pusher.com/docs/channels/using_channels/user-authentication/).
*/
signin(): void {
this.pusher.signin();
}
/**
* Listen for an event on a channel instance.
*/
listen(name: string, event: string, callback: Function): PusherChannel {
return this.channel(name).listen(event, callback);
}
/**
* Get a channel instance by name.
*/
channel(name: string): PusherChannel {
if (!this.channels[name]) {
this.channels[name] = new PusherChannel(this.pusher, name, this.options);
}
return this.channels[name];
}
/**
* Get a private channel instance by name.
*/
privateChannel(name: string): PusherChannel {
if (!this.channels['private-' + name]) {
this.channels['private-' + name] = new PusherPrivateChannel(this.pusher, 'private-' + name, this.options);
}
return this.channels['private-' + name];
}
/**
* Get a private encrypted channel instance by name.
*/
encryptedPrivateChannel(name: string): PusherChannel {
if (!this.channels['private-encrypted-' + name]) {
this.channels['private-encrypted-' + name] = new PusherEncryptedPrivateChannel(
this.pusher,
'private-encrypted-' + name,
this.options
);
}
return this.channels['private-encrypted-' + name];
}
/**
* Get a presence channel instance by name.
*/
presenceChannel(name: string): PresenceChannel {
if (!this.channels['presence-' + name]) {
this.channels['presence-' + name] = new PusherPresenceChannel(
this.pusher,
'presence-' + name,
this.options
);
}
return this.channels['presence-' + name];
}
/**
* Leave the given channel, as well as its private and presence variants.
*/
leave(name: string): void {
let channels = [name, 'private-' + name, 'private-encrypted-' + name, 'presence-' + name];
channels.forEach((name: string, index: number) => {
this.leaveChannel(name);
});
}
/**
* Leave the given channel.
*/
leaveChannel(name: string): void {
if (this.channels[name]) {
this.channels[name].unsubscribe();
delete this.channels[name];
}
}
/**
* Get the socket ID for the connection.
*/
socketId(): string {
return this.pusher.connection.socket_id;
}
/**
* Disconnect Pusher connection.
*/
disconnect(): void {
this.pusher.disconnect();
}
}