Skip to content

Commit a0db677

Browse files
authored
[Streaming] Add working ws websocket implementation for Node environment (#1334)
* add working ws websocket impl * remove unused constants
1 parent cf497da commit a0db677

16 files changed

+307
-14
lines changed

libraries/botframework-streaming/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"devDependencies": {
1111
"@types/chai": "^4.1.7",
1212
"@types/node": "^10.12.18",
13+
"@types/ws": "^6.0.3",
1314
"chai": "^4.2.0",
1415
"mocha": "^6.2.0",
1516
"nyc": "^14.1.1",
@@ -22,7 +23,8 @@
2223
"dependencies": {
2324
"promise.prototype.finally": "^3.1.0",
2425
"uuid": "^3.3.2",
25-
"watershed": "^0.4.0"
26+
"watershed": "^0.4.0",
27+
"ws": "^7.1.2"
2628
},
2729
"engines": {
2830
"node": ">12.3"

libraries/botframework-streaming/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ export {
2020
NodeWebSocketFactoryBase,
2121
WebSocketClient,
2222
WebSocketServer,
23+
WsNodeWebSocket,
24+
WsNodeWebSocketFactory,
2325
} from './webSocket';

libraries/botframework-streaming/src/interfaces/ISocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* with the WebSocket server or client.
1212
*/
1313
export interface ISocket {
14-
isConnected(): boolean;
14+
isConnected: boolean;
1515
write(buffer: Buffer);
1616
connect(serverAddress: string): Promise<void>;
1717
close();

libraries/botframework-streaming/src/webSocket/browserWebSocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class BrowserWebSocket implements ISocket {
5252
/**
5353
* True if the socket is currently connected.
5454
*/
55-
public isConnected(): boolean {
55+
public get isConnected(): boolean {
5656
return this.webSocket.readyState === 1;
5757
}
5858

libraries/botframework-streaming/src/webSocket/factories/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
export * from './nodeWebSocketFactory';
1010
export * from './nodeWebSocketFactoryBase';
11+
export * from './wsNodeWebSocketFactory';

libraries/botframework-streaming/src/webSocket/factories/nodeWebSocketFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class NodeWebSocketFactory extends NodeWebSocketFactoryBase {
1616
constructor() {
1717
super();
1818
}
19-
19+
2020
/**
2121
* Creates a NodeWebSocket instance.
2222
* @param req
@@ -26,7 +26,7 @@ export class NodeWebSocketFactory extends NodeWebSocketFactoryBase {
2626
public createWebSocket(req: IncomingMessage, socket: Socket, head: Buffer): NodeWebSocket {
2727
const s = new NodeWebSocket();
2828
s.create(req, socket, head);
29-
29+
3030
return s;
3131
}
3232
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @module botframework-streaming
3+
*/
4+
/**
5+
* Copyright (c) Microsoft Corporation. All rights reserved.
6+
* Licensed under the MIT License.
7+
*/
8+
9+
import { IncomingMessage } from 'http';
10+
import { Socket } from 'net';
11+
12+
import { WsNodeWebSocket } from '../wsNodeWebSocket';
13+
14+
export class WsNodeWebSocketFactory {
15+
/**
16+
* Creates a WsNodeWebSocket instance.
17+
* @param req
18+
* @param socket
19+
* @param head
20+
*/
21+
public async createWebSocket(req: IncomingMessage, socket: Socket, head: Buffer): Promise<WsNodeWebSocket> {
22+
const s = new WsNodeWebSocket();
23+
await s.create(req, socket, head);
24+
25+
return s;
26+
}
27+
}

libraries/botframework-streaming/src/webSocket/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './nodeWebSocket';
1313
export * from './webSocketClient';
1414
export * from './webSocketServer';
1515
export * from './webSocketTransport';
16+
export * from './wsNodeWebSocket';

libraries/botframework-streaming/src/webSocket/nodeWebSocket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class NodeWebSocket implements ISocket {
4141
/**
4242
* True if the socket is currently connected.
4343
*/
44-
public isConnected(): boolean {
44+
public get isConnected(): boolean {
4545
return this.connected;
4646
}
4747

libraries/botframework-streaming/src/webSocket/webSocketTransport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class WebSocketTransport implements ITransportSender, ITransportReceiver
4848
* @param buffer The buffered data to send out over the connection.
4949
*/
5050
public send(buffer: Buffer): number {
51-
if (this._socket && this._socket.isConnected()) {
51+
if (this._socket && this._socket.isConnected) {
5252
this._socket.write(buffer);
5353

5454
return buffer.length;
@@ -61,14 +61,14 @@ export class WebSocketTransport implements ITransportSender, ITransportReceiver
6161
* Returns true if the transport is connected to a socket.
6262
*/
6363
public isConnected(): boolean {
64-
return this._socket.isConnected();
64+
return this._socket.isConnected;
6565
}
6666

6767
/**
6868
* Close the socket this transport is connected to.
6969
*/
7070
public close(): void {
71-
if (this._socket && this._socket.isConnected()) {
71+
if (this._socket && this._socket.isConnected) {
7272
this._socket.close();
7373
}
7474
}

0 commit comments

Comments
 (0)