lowercasing event names

This commit is contained in:
Pawel Kadluczka 2017-09-20 18:19:02 -07:00
parent 5ca089e33e
commit 20d4d70cc7
12 changed files with 79 additions and 88 deletions

View File

@ -152,8 +152,8 @@ describe("Connection", () => {
return Promise.reject(""); return Promise.reject("");
}, },
stop(): void { }, stop(): void { },
onDataReceived: undefined, onreceive: undefined,
onClosed: undefined, onclose: undefined,
} }
let options: IHttpConnectionOptions = { let options: IHttpConnectionOptions = {
@ -249,8 +249,8 @@ describe("Connection", () => {
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> { return Promise.resolve(transportTransferMode); }, connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> { return Promise.resolve(transportTransferMode); },
send(data: any): Promise<void> { return Promise.resolve(); }, send(data: any): Promise<void> { return Promise.resolve(); },
stop(): void {}, stop(): void {},
onDataReceived: null, onreceive: null,
onClosed: null, onclose: null,
mode: transportTransferMode mode: transportTransferMode
} as ITransport; } as ITransport;

View File

@ -117,7 +117,7 @@ describe("HubConnection", () => {
let hubConnection = new HubConnection(connection); let hubConnection = new HubConnection(connection);
let invokePromise = hubConnection.invoke("testMethod"); let invokePromise = hubConnection.invoke("testMethod");
// Typically this would be called by the transport // Typically this would be called by the transport
connection.onClosed(new Error("Connection lost")); connection.onclose(new Error("Connection lost"));
let ex = await captureException(async () => await invokePromise); let ex = await captureException(async () => await invokePromise);
expect(ex.message).toBe("Connection lost"); expect(ex.message).toBe("Connection lost");
@ -130,7 +130,7 @@ describe("HubConnection", () => {
let invokePromise = hubConnection.invoke("testMethod"); let invokePromise = hubConnection.invoke("testMethod");
connection.receive({ type: 2, invocationId: connection.lastInvocationId, item: null }); connection.receive({ type: 2, invocationId: connection.lastInvocationId, item: null });
connection.onClosed(); connection.onclose();
let ex = await captureException(async () => await invokePromise); let ex = await captureException(async () => await invokePromise);
expect(ex.message).toBe("Streaming methods must be invoked using HubConnection.stream"); expect(ex.message).toBe("Streaming methods must be invoked using HubConnection.stream");
@ -350,7 +350,7 @@ describe("HubConnection", () => {
.subscribe(observer); .subscribe(observer);
// Typically this would be called by the transport // Typically this would be called by the transport
connection.onClosed(new Error("Connection lost")); connection.onclose(new Error("Connection lost"));
let ex = await captureException(async () => await observer.completed); let ex = await captureException(async () => await observer.completed);
expect(ex.message).toEqual("Error: Connection lost"); expect(ex.message).toEqual("Error: Connection lost");
@ -397,10 +397,10 @@ describe("HubConnection", () => {
let connection = new TestConnection(); let connection = new TestConnection();
let hubConnection = new HubConnection(connection); let hubConnection = new HubConnection(connection);
let invocations = 0; let invocations = 0;
hubConnection.onClosed(e => invocations++); hubConnection.onclose(e => invocations++);
hubConnection.onClosed(e => invocations++); hubConnection.onclose(e => invocations++);
// Typically this would be called by the transport // Typically this would be called by the transport
connection.onClosed(); connection.onclose();
expect(invocations).toBe(2); expect(invocations).toBe(2);
}); });
@ -408,20 +408,20 @@ describe("HubConnection", () => {
let connection = new TestConnection(); let connection = new TestConnection();
let hubConnection = new HubConnection(connection); let hubConnection = new HubConnection(connection);
let error: Error; let error: Error;
hubConnection.onClosed(e => error = e); hubConnection.onclose(e => error = e);
// Typically this would be called by the transport // Typically this would be called by the transport
connection.onClosed(new Error("Test error.")); connection.onclose(new Error("Test error."));
expect(error.message).toBe("Test error."); expect(error.message).toBe("Test error.");
}); });
it("ignores null callbacks", async () => { it("ignores null callbacks", async () => {
let connection = new TestConnection(); let connection = new TestConnection();
let hubConnection = new HubConnection(connection); let hubConnection = new HubConnection(connection);
hubConnection.onClosed(null); hubConnection.onclose(null);
hubConnection.onClosed(undefined); hubConnection.onclose(undefined);
// Typically this would be called by the transport // Typically this would be called by the transport
connection.onClosed(); connection.onclose();
// expect no errors // expect no errors
}); });
}); });
@ -447,18 +447,18 @@ class TestConnection implements IConnection {
}; };
stop(): void { stop(): void {
if (this.onClosed) { if (this.onclose) {
this.onClosed(); this.onclose();
} }
}; };
receive(data: any): void { receive(data: any): void {
let payload = JSON.stringify(data); let payload = JSON.stringify(data);
this.onDataReceived(TextMessageFormat.write(payload)); this.onreceive(TextMessageFormat.write(payload));
} }
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: ConnectionClosed; onclose: ConnectionClosed;
sentData: [any]; sentData: [any];
lastInvocationId: string; lastInvocationId: string;
}; };

View File

@ -67,8 +67,8 @@ export class HttpConnection implements IConnection {
this.url += (this.url.indexOf("?") == -1 ? "?" : "&") + `id=${this.connectionId}`; this.url += (this.url.indexOf("?") == -1 ? "?" : "&") + `id=${this.connectionId}`;
this.transport = this.createTransport(this.options.transport, negotiateResponse.availableTransports); this.transport = this.createTransport(this.options.transport, negotiateResponse.availableTransports);
this.transport.onDataReceived = this.onDataReceived; this.transport.onreceive = this.onreceive;
this.transport.onClosed = e => this.stopConnection(true, e); this.transport.onclose = e => this.stopConnection(true, e);
let requestedTransferMode = let requestedTransferMode =
this.features.transferMode === TransferMode.Binary this.features.transferMode === TransferMode.Binary
@ -151,8 +151,8 @@ export class HttpConnection implements IConnection {
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
if (raiseClosed && this.onClosed) { if (raiseClosed && this.onclose) {
this.onClosed(error); this.onclose(error);
} }
} }
@ -182,6 +182,6 @@ export class HttpConnection implements IConnection {
return normalizedUrl; return normalizedUrl;
} }
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: ConnectionClosed; onclose: ConnectionClosed;
} }

View File

@ -41,12 +41,8 @@ export class HubConnection {
this.logger = LoggerFactory.createLogger(options.logging); this.logger = LoggerFactory.createLogger(options.logging);
this.protocol = options.protocol || new JsonHubProtocol(); this.protocol = options.protocol || new JsonHubProtocol();
this.connection.onDataReceived = data => { this.connection.onreceive = (data: any) => this.processIncomingData(data);
this.onDataReceived(data); this.connection.onclose = (error?: Error) => this.connectionClosed(error);
};
this.connection.onClosed = (error?: Error) => {
this.onConnectionClosed(error);
}
this.callbacks = new Map<string, (invocationEvent: CompletionMessage | ResultMessage) => void>(); this.callbacks = new Map<string, (invocationEvent: CompletionMessage | ResultMessage) => void>();
this.methods = new Map<string, ((...args: any[]) => void)[]>(); this.methods = new Map<string, ((...args: any[]) => void)[]>();
@ -54,7 +50,7 @@ export class HubConnection {
this.id = 0; this.id = 0;
} }
private onDataReceived(data: any) { private processIncomingData(data: any) {
// Parse the messages // Parse the messages
let messages = this.protocol.parseMessages(data); let messages = this.protocol.parseMessages(data);
@ -95,7 +91,7 @@ export class HubConnection {
} }
} }
private onConnectionClosed(error?: Error) { private connectionClosed(error?: Error) {
let errorCompletionMessage = <CompletionMessage>{ let errorCompletionMessage = <CompletionMessage>{
type: MessageType.Completion, type: MessageType.Completion,
invocationId: "-1", invocationId: "-1",
@ -238,7 +234,7 @@ export class HubConnection {
} }
} }
onClosed(callback: ConnectionClosed) { onclose(callback: ConnectionClosed) {
if (callback) { if (callback) {
this.closedCallbacks.push(callback); this.closedCallbacks.push(callback);
} }

View File

@ -11,6 +11,6 @@ export interface IConnection {
send(data: any): Promise<void>; send(data: any): Promise<void>;
stop(): void; stop(): void;
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: ConnectionClosed; onclose: ConnectionClosed;
} }

View File

@ -21,8 +21,8 @@ export interface ITransport {
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode>; connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode>;
send(data: any): Promise<void>; send(data: any): Promise<void>;
stop(): void; stop(): void;
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: TransportClosed; onclose: TransportClosed;
} }
export class WebSocketTransport implements ITransport { export class WebSocketTransport implements ITransport {
@ -55,19 +55,19 @@ export class WebSocketTransport implements ITransport {
webSocket.onmessage = (message: MessageEvent) => { webSocket.onmessage = (message: MessageEvent) => {
this.logger.log(LogLevel.Trace, `(WebSockets transport) data received: ${message.data}`); this.logger.log(LogLevel.Trace, `(WebSockets transport) data received: ${message.data}`);
if (this.onDataReceived) { if (this.onreceive) {
this.onDataReceived(message.data); this.onreceive(message.data);
} }
} }
webSocket.onclose = (event: CloseEvent) => { webSocket.onclose = (event: CloseEvent) => {
// webSocket will be null if the transport did not start successfully // webSocket will be null if the transport did not start successfully
if (this.onClosed && this.webSocket) { if (this.onclose && this.webSocket) {
if (event.wasClean === false || event.code !== 1000) { if (event.wasClean === false || event.code !== 1000) {
this.onClosed(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`)); this.onclose(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`));
} }
else { else {
this.onClosed(); this.onclose();
} }
} }
} }
@ -90,8 +90,8 @@ export class WebSocketTransport implements ITransport {
} }
} }
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: TransportClosed; onclose: TransportClosed;
} }
export class ServerSentEventsTransport implements ITransport { export class ServerSentEventsTransport implements ITransport {
@ -116,13 +116,13 @@ export class ServerSentEventsTransport implements ITransport {
try { try {
eventSource.onmessage = (e: MessageEvent) => { eventSource.onmessage = (e: MessageEvent) => {
if (this.onDataReceived) { if (this.onreceive) {
try { try {
this.logger.log(LogLevel.Trace, `(SSE transport) data received: ${e.data}`); this.logger.log(LogLevel.Trace, `(SSE transport) data received: ${e.data}`);
this.onDataReceived(e.data); this.onreceive(e.data);
} catch (error) { } catch (error) {
if (this.onClosed) { if (this.onclose) {
this.onClosed(error); this.onclose(error);
} }
return; return;
} }
@ -133,8 +133,8 @@ export class ServerSentEventsTransport implements ITransport {
reject(); reject();
// don't report an error if the transport did not start successfully // don't report an error if the transport did not start successfully
if (this.eventSource && this.onClosed) { if (this.eventSource && this.onclose) {
this.onClosed(new Error(e.message || "Error occurred")); this.onclose(new Error(e.message || "Error occurred"));
} }
} }
@ -162,8 +162,8 @@ export class ServerSentEventsTransport implements ITransport {
} }
} }
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: TransportClosed; onclose: TransportClosed;
} }
export class LongPollingTransport implements ITransport { export class LongPollingTransport implements ITransport {
@ -201,7 +201,7 @@ export class LongPollingTransport implements ITransport {
pollXhr.onload = () => { pollXhr.onload = () => {
if (pollXhr.status == 200) { if (pollXhr.status == 200) {
if (this.onDataReceived) { if (this.onreceive) {
try { try {
let response = transferMode === TransferMode.Text let response = transferMode === TransferMode.Text
? pollXhr.responseText ? pollXhr.responseText
@ -209,14 +209,14 @@ export class LongPollingTransport implements ITransport {
if (response) { if (response) {
this.logger.log(LogLevel.Trace, `(LongPolling transport) data received: ${response}`); this.logger.log(LogLevel.Trace, `(LongPolling transport) data received: ${response}`);
this.onDataReceived(response); this.onreceive(response);
} }
else { else {
this.logger.log(LogLevel.Information, "(LongPolling transport) timed out"); this.logger.log(LogLevel.Information, "(LongPolling transport) timed out");
} }
} catch (error) { } catch (error) {
if (this.onClosed) { if (this.onclose) {
this.onClosed(error); this.onclose(error);
} }
return; return;
} }
@ -224,21 +224,21 @@ export class LongPollingTransport implements ITransport {
this.poll(url, transferMode); this.poll(url, transferMode);
} }
else if (this.pollXhr.status == 204) { else if (this.pollXhr.status == 204) {
if (this.onClosed) { if (this.onclose) {
this.onClosed(); this.onclose();
} }
} }
else { else {
if (this.onClosed) { if (this.onclose) {
this.onClosed(new HttpError(pollXhr.statusText, pollXhr.status)); this.onclose(new HttpError(pollXhr.statusText, pollXhr.status));
} }
} }
}; };
pollXhr.onerror = () => { pollXhr.onerror = () => {
if (this.onClosed) { if (this.onclose) {
// network related error or denied cross domain request // network related error or denied cross domain request
this.onClosed(new Error("Sending HTTP request failed.")); this.onclose(new Error("Sending HTTP request failed."));
} }
}; };
@ -270,8 +270,8 @@ export class LongPollingTransport implements ITransport {
} }
} }
onDataReceived: DataReceived; onreceive: DataReceived;
onClosed: TransportClosed; onclose: TransportClosed;
} }
const headers = new Map<string, string>(); const headers = new Map<string, string>();

View File

@ -10,14 +10,14 @@ describe('connection', function () {
var connection = new signalR.HttpConnection(ECHOENDPOINT_URL); var connection = new signalR.HttpConnection(ECHOENDPOINT_URL);
var received = ""; var received = "";
connection.onDataReceived = function (data) { connection.onreceive = function (data) {
received += data; received += data;
if (data == message) { if (data == message) {
connection.stop(); connection.stop();
} }
}; };
connection.onClosed = function (error) { connection.onclose = function (error) {
expect(error).toBeUndefined(); expect(error).toBeUndefined();
done(); done();
}; };
@ -42,14 +42,14 @@ describe('connection', function () {
}); });
var received = ""; var received = "";
connection.onDataReceived = function (data) { connection.onreceive = function (data) {
received += data; received += data;
if (data == message) { if (data == message) {
connection.stop(); connection.stop();
} }
}; };
connection.onClosed = function (error) { connection.onclose = function (error) {
expect(error).toBeUndefined(); expect(error).toBeUndefined();
done(); done();
}; };

View File

@ -18,7 +18,7 @@ describe('hubConnection', function () {
logging: signalR.LogLevel.Trace logging: signalR.LogLevel.Trace
}; };
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options); var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.onClosed(function (error) { hubConnection.onclose(function (error) {
expect(error).toBe(undefined); expect(error).toBe(undefined);
done(); done();
}); });
@ -46,7 +46,7 @@ describe('hubConnection', function () {
}; };
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options); var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.onClosed(function (error) { hubConnection.onclose(function (error) {
expect(error).toBe(undefined); expect(error).toBe(undefined);
done(); done();
}); });
@ -172,7 +172,7 @@ describe('hubConnection', function () {
}; };
var hubConnection = new signalR.HubConnection('http://' + document.location.host + '/uncreatable', options); var hubConnection = new signalR.HubConnection('http://' + document.location.host + '/uncreatable', options);
hubConnection.onClosed(function (error) { hubConnection.onclose(function (error) {
expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]); expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]);
done(); done();
}); });

View File

@ -18,17 +18,16 @@
<script> <script>
let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets; let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets;
let logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); let logger = new signalR.ConsoleLogger(signalR.LogLevel.Information);
let http = new signalR.HttpConnection(`http://${document.location.host}/chat`, { transport: transportType, logger: logger }); let connection = new signalR.HubConnection('/chat', { transport: transportType, logger: logger });
let connection = new signalR.HubConnection(http, logger);
connection.onClosed = e => { connection.onclose(e => {
if (e) { if (e) {
appendLine('Connection closed with error: ' + e, 'red'); appendLine('Connection closed with error: ' + e, 'red');
} }
else { else {
appendLine('Disconnected', 'green'); appendLine('Disconnected', 'green');
} }
}; });
connection.on('SetUsersOnline', usersOnline => { connection.on('SetUsersOnline', usersOnline => {
usersOnline.forEach(user => addUserOnline(user)); usersOnline.forEach(user => addUserOnline(user));

View File

@ -104,20 +104,19 @@ click('connect', function(event) {
connectButton.disabled = true; connectButton.disabled = true;
disconnectButton.disabled = false; disconnectButton.disabled = false;
console.log('http://' + document.location.host + '/' + hubRoute); console.log('http://' + document.location.host + '/' + hubRoute);
let http = new signalR.HttpConnection('http://' + document.location.host + '/' + hubRoute, { transport: transportType, logger: logger }); connection = new signalR.HubConnection(hubRoute, logger, { transport: transportType, logger: logger });
connection = new signalR.HubConnection(http, logger);
connection.on('Send', function(msg) { connection.on('Send', function(msg) {
addLine('message-list', msg); addLine('message-list', msg);
}); });
connection.onClosed = function(e) { connection.onclose(function(e) {
if (e) { if (e) {
addLine('message-list', 'Connection closed with error: ' + e, 'red'); addLine('message-list', 'Connection closed with error: ' + e, 'red');
} }
else { else {
addLine('message-list', 'Disconnected', 'green'); addLine('message-list', 'Disconnected', 'green');
} }
} });
connection.start() connection.start()
.then(function() { .then(function() {

View File

@ -33,7 +33,7 @@
let url = 'http://' + document.location.host + '/chat'; let url = 'http://' + document.location.host + '/chat';
let connection = new signalR.HttpConnection(url, { transport: transportType, logger: new signalR.ConsoleLogger(signalR.LogLevel.Information) }); let connection = new signalR.HttpConnection(url, { transport: transportType, logger: new signalR.ConsoleLogger(signalR.LogLevel.Information) });
connection.onDataReceived = function(data) { connection.onreceive = function(data) {
let child = document.createElement('li'); let child = document.createElement('li');
child.innerText = data; child.innerText = data;
document.getElementById('messages').appendChild(child); document.getElementById('messages').appendChild(child);

View File

@ -54,8 +54,6 @@
let invocationCounter = 0; let invocationCounter = 0;
document.getElementById('transportName').innerHTML = signalR.TransportType[transportType]; document.getElementById('transportName').innerHTML = signalR.TransportType[transportType];
let url = 'http://' + document.location.host + '/streaming';
let connection = null; let connection = null;
click('clearButton', function () { click('clearButton', function () {
@ -67,17 +65,16 @@
}); });
click('connectButton', function () { click('connectButton', function () {
let http = new signalR.HttpConnection(url, { transport: transportType, logger: logger }); connection = new signalR.HubConnection('/streaming', { transport: transportType, logger: logger });
connection = new signalR.HubConnection(http, logger);
connection.onClosed = function () { connection.onclose(function () {
channelButton.disabled = true; channelButton.disabled = true;
observableButton.disabled = true; observableButton.disabled = true;
connectButton.disabled = false; connectButton.disabled = false;
disconnectButton.disabled = true; disconnectButton.disabled = true;
addLine('resultsList', 'disconnected', 'green'); addLine('resultsList', 'disconnected', 'green');
}; });
connection.start() connection.start()
.then(function () { .then(function () {