Skip to content

Commit 0393ac5

Browse files
committed
Tweaked more log messages
1 parent cbdc4f8 commit 0393ac5

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ vendor/
88
.phpunit.result.cache
99
yarn-error.log
1010
.DS_Store
11+
_*

js-src/Channel.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Channel as BaseChannel } from 'laravel-echo/src/channel/channel';
33
import { PresenceChannel } from "laravel-echo/src/channel";
44
import { Websocket } from "./Websocket";
55

6+
const LOG_PREFIX = '[LE-AG-Channel]';
7+
68
/**
79
* This class represents a Pusher channel.
810
*/
@@ -45,20 +47,26 @@ export class Channel extends BaseChannel implements PresenceChannel {
4547
* Subscribe to a Pusher channel.
4648
*/
4749
subscribe(): any {
50+
this.options["debug"] && console.log(`${LOG_PREFIX} subscribe for channel ${this.name} ...`);
51+
4852
this.socket.subscribe(this)
4953
}
5054

5155
/**
5256
* Unsubscribe from a Pusher channel.
5357
*/
5458
unsubscribe(): void {
59+
this.options["debug"] && console.log(`${LOG_PREFIX} unsubscribe for channel ${this.name} ...`);
60+
5561
this.socket.unsubscribe(this);
5662
}
5763

5864
/**
5965
* Listen for an event on the channel instance.
6066
*/
61-
listen(event: string, callback: Function): Channel {
67+
listen(event: string, callback: Function): this {
68+
this.options["debug"] && console.log(`${LOG_PREFIX} listen to ${event} for channel ${this.name} ...`);
69+
6270
this.on(this.eventFormatter.format(event), callback);
6371

6472
return this;
@@ -67,7 +75,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
6775
/**
6876
* Stop listening for an event on the channel instance.
6977
*/
70-
stopListening(event: string, callback?: Function): Channel {
78+
stopListening(event: string, callback?: Function): this {
79+
this.options["debug"] && console.log(`${LOG_PREFIX} stop listening to ${event} for channel ${this.name} ...`);
80+
7181
this.socket.unbindEvent(this, event, callback)
7282

7383
return this;
@@ -76,7 +86,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
7686
/**
7787
* Register a callback to be called anytime a subscription succeeds.
7888
*/
79-
subscribed(callback: Function): Channel {
89+
subscribed(callback: Function): this {
90+
this.options["debug"] && console.log(`${LOG_PREFIX} subscribed for channel ${this.name} ...`);
91+
8092
this.on('subscription_succeeded', () => {
8193
callback();
8294
});
@@ -87,7 +99,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
8799
/**
88100
* Register a callback to be called anytime a subscription error occurs.
89101
*/
90-
error(callback: Function): Channel {
102+
error(callback: Function): this {
103+
this.options["debug"] && console.log(`${LOG_PREFIX} error for channel ${this.name} ...`);
104+
91105
this.on('error', (status) => {
92106
callback(status);
93107
});
@@ -99,12 +113,14 @@ export class Channel extends BaseChannel implements PresenceChannel {
99113
* Bind a channel to an event.
100114
*/
101115
on(event: string, callback: Function): Channel {
116+
this.options["debug"] && console.log(`${LOG_PREFIX} on ${event} for channel ${this.name} ...`);
117+
102118
this.socket.bind(this, event, callback)
103119

104120
return this;
105121
}
106122

107-
whisper(event: string, data: object): Channel {
123+
whisper(event: string, data: object): this {
108124
let channel = this.name;
109125
let formattedEvent = "client-" + event;
110126
this.socket.send({
@@ -116,7 +132,7 @@ export class Channel extends BaseChannel implements PresenceChannel {
116132
return this;
117133
}
118134

119-
here(callback: Function): Channel {
135+
here(callback: Function): this {
120136
// TODO: implement
121137

122138
return this
@@ -125,7 +141,7 @@ export class Channel extends BaseChannel implements PresenceChannel {
125141
/**
126142
* Listen for someone joining the channel.
127143
*/
128-
joining(callback: Function): Channel {
144+
joining(callback: Function): this {
129145
// TODO: implement
130146

131147
return this
@@ -134,9 +150,11 @@ export class Channel extends BaseChannel implements PresenceChannel {
134150
/**
135151
* Listen for someone leaving the channel.
136152
*/
137-
leaving(callback: Function): Channel {
153+
leaving(callback: Function): this {
138154
// TODO: implement
139155

140156
return this
141157
}
142158
}
159+
160+
export { PresenceChannel };

js-src/Connector.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {Channel} from "./Channel";
44

55
export const broadcaster = (options: object): Connector => new Connector(options);
66

7+
const LOG_PREFIX = '[LE-AG-Connector]';
8+
79
export class Connector extends BaseConnector {
810
/**
911
* The Socket.io connection instance.
@@ -15,10 +17,19 @@ export class Connector extends BaseConnector {
1517
*/
1618
channels: { [name: string]: Channel } = {};
1719

20+
/**
21+
* Create a new class instance.
22+
*/
23+
constructor(options: any) {
24+
super(options);
25+
}
26+
1827
/**
1928
* Create a fresh Socket.io connection.
2029
*/
2130
connect(): void {
31+
this.options.debug && console.log(LOG_PREFIX + 'Connect ...' );
32+
2233
this.socket = new Websocket(this.options);
2334

2435
return;
@@ -103,6 +114,8 @@ export class Connector extends BaseConnector {
103114
* Disconnect socket connection.
104115
*/
105116
disconnect(): void {
117+
this.options.debug && console.log(LOG_PREFIX + 'Disconnect ...' );
118+
106119
this.socket.close();
107120
}
108121
}

js-src/Websocket.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type Options = { authEndpoint: string, host: string, bearerToken: string,
55

66
export type MessageBody = { event: string, channel?: string, data: object };
77

8-
const LOG_PREFIX = '[AG-WS]';
8+
const LOG_PREFIX = '[LE-AG-Websocket]';
99

1010
export class Websocket {
1111
buffer: Array<object> = [];
@@ -27,11 +27,22 @@ export class Websocket {
2727

2828
private pingInterval: NodeJS.Timeout;
2929

30+
constructor(options: Options) {
31+
this.options = options;
32+
33+
this.connect(this.options.host);
34+
35+
return this;
36+
}
37+
3038
private connect(host: string): void {
39+
3140
if (!host) {
3241
this.options.debug && console.error(LOG_PREFIX + `Cannont connect without host !`);
42+
3343
return;
3444
}
45+
3546
this.options.debug && console.log(LOG_PREFIX + `Trying to connect to ${host}...` );
3647

3748
this.websocket = new WebSocket(host);
@@ -132,14 +143,6 @@ export class Websocket {
132143
});
133144
}
134145

135-
constructor(options: Options) {
136-
this.options = options;
137-
138-
this.connect(this.options.host);
139-
140-
return this;
141-
}
142-
143146
protected parseMessage(body: string): MessageBody {
144147
try {
145148
return JSON.parse(body);
@@ -181,6 +184,8 @@ export class Websocket {
181184
if (this.getSocketId()) {
182185
this.actuallySubscribe(channel);
183186
} else {
187+
this.options.debug && console.log(`${LOG_PREFIX} subscribe - push channel backlog for channel ${channel.name}`);
188+
184189
this.channelBacklog.push(channel);
185190
}
186191
}
@@ -225,6 +230,8 @@ export class Websocket {
225230
}
226231

227232
unsubscribe(channel: Channel): void {
233+
this.options.debug && console.log(`${LOG_PREFIX} unsubscribe for channel ${channel.name}`);
234+
228235
this.send({
229236
event: 'unsubscribe',
230237
data: {
@@ -238,10 +245,14 @@ export class Websocket {
238245
}
239246

240247
on(event: string, callback: Function = null): void {
248+
this.options.debug && console.log(`${LOG_PREFIX} on event ${event} ...`);
249+
241250
this.internalListeners[event] = callback;
242251
}
243252

244253
bind(channel: Channel, event: string, callback: Function): void {
254+
this.options.debug && console.log(`${LOG_PREFIX} bind event ${event} for channel ${channel.name} ...`);
255+
245256
if (!this.listeners[channel.name]) {
246257
this.listeners[channel.name] = {};
247258
}
@@ -250,6 +261,8 @@ export class Websocket {
250261
}
251262

252263
unbindEvent(channel: Channel, event: string, callback: Function = null): void {
264+
this.options.debug && console.log(`${LOG_PREFIX} unbind event ${event} for channel ${channel.name} ...`);
265+
253266
if (this.internalListeners[event] && (callback === null || this.internalListeners[event] === callback)) {
254267
delete this.internalListeners[event];
255268
}

0 commit comments

Comments
 (0)