Skip to content
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

Add support for defining WebSocket protocol to use with Peer instance #987

Open
wants to merge 3 commits 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
2 changes: 2 additions & 0 deletions lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PeerOptions implements PeerJSOption {
config?: any;
secure?: boolean;
pingInterval?: number;
protocol?: string | string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this only accepts strings starting with a prefix.
What do you think about the type: x-${Lowercase<string>}?

That way PeerJS can still set its own protocols without interference from user-protocols.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then this would force the hands of users to either (1) use protcols named x-${Lowercase<string>}, which would not always be possible, since the user might be using a WebSocket server not controlled by then, and that WebSocket server might have a protocol name that doesn't confine to the above types...

referrerPolicy?: ReferrerPolicy;
logFunction?: (logLevel: LogLevel, ...rest: any[]) => void;
}
Expand Down Expand Up @@ -240,6 +241,7 @@ export class Peer extends EventEmitter<PeerEvents> {
this._options.path!,
this._options.key!,
this._options.pingInterval,
this._options.protocol,
);

socket.on(SocketEventType.Message, (data: ServerMessage) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class Socket extends EventEmitter {
path: string,
key: string,
private readonly pingInterval: number = 5000,
private readonly protocol: string | string[] | undefined = undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's skip undefined and initialize this to []

) {
super();

Expand All @@ -39,7 +40,7 @@ export class Socket extends EventEmitter {
return;
}

this._socket = new WebSocket(wsUrl + "&version=" + version);
this._socket = new WebSocket(wsUrl + "&version=" + version, this.protocol);
this._disconnected = false;

this._socket.onmessage = (event) => {
Expand Down