Freigeben über


HubConnection class

Represents a connection to a SignalR Hub.

Properties

baseUrl

Indicates the url of the HubConnection to the server. Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or Reconnecting states.

connectionId

Represents the connection id of the HubConnection on the server. The connection id will be null when the connection is either in the disconnected state or if the negotiation step was skipped.

keepAliveIntervalInMilliseconds

Default interval at which to ping the server. The default value is 15,000 milliseconds (15 seconds). Allows the server to detect hard disconnects (like when a client unplugs their computer). The ping will happen at most as often as the server pings. If the server pings every 5 seconds, a value lower than 5 will ping every 5 seconds.

serverTimeoutInMilliseconds

The server timeout in milliseconds. If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error. The default timeout value is 30,000 milliseconds (30 seconds).

state

Indicates the state of the HubConnection to the server.

Methods

create(IConnection, ILogger, IHubProtocol, IRetryPolicy)
invoke<T>(string, any[])

Invokes a hub method on the server using the specified name and arguments. The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of resolving the Promise.

off(string)

Removes all handlers for the specified hub method.

off(string, (args: any[]) => void)

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on(string, (args: any[]) => void). Passing a different instance (even if the function body is the same) will not remove the handler.

on(string, (args: any[]) => void)

Registers a handler that will be invoked when the hub method with the specified method name is invoked.

onclose((error?: Error) => void)

Registers a handler that will be invoked when the connection is closed.

onreconnected((connectionId?: string) => void)

Registers a handler that will be invoked when the connection successfully reconnects.

onreconnecting((error?: Error) => void)

Registers a handler that will be invoked when the connection starts reconnecting.

send(string, any[])

Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still be processing the invocation.

start()

Starts the connection.

stop()

Stops the connection.

stream<T>(string, any[])

Invokes a streaming hub method on the server using the specified name and arguments.

Property Details

baseUrl

Indicates the url of the HubConnection to the server. Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or Reconnecting states.

string baseUrl

Property Value

string

connectionId

Represents the connection id of the HubConnection on the server. The connection id will be null when the connection is either in the disconnected state or if the negotiation step was skipped.

string | null connectionId

Property Value

string | null

keepAliveIntervalInMilliseconds

Default interval at which to ping the server. The default value is 15,000 milliseconds (15 seconds). Allows the server to detect hard disconnects (like when a client unplugs their computer). The ping will happen at most as often as the server pings. If the server pings every 5 seconds, a value lower than 5 will ping every 5 seconds.

public keepAliveIntervalInMilliseconds: number

Property Value

number

serverTimeoutInMilliseconds

The server timeout in milliseconds. If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error. The default timeout value is 30,000 milliseconds (30 seconds).

public serverTimeoutInMilliseconds: number

Property Value

number

state

Indicates the state of the HubConnection to the server.

HubConnectionState state

Property Value

Method Details

create(IConnection, ILogger, IHubProtocol, IRetryPolicy)

static function create(connection: IConnection, logger: ILogger, protocol: IHubProtocol, reconnectPolicy?: IRetryPolicy): HubConnection

Parameters

connection

IConnection

logger
ILogger
protocol
IHubProtocol
reconnectPolicy
IRetryPolicy

Returns

invoke<T>(string, any[])

Invokes a hub method on the server using the specified name and arguments. The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of resolving the Promise.

function invoke<T>(methodName: string, args: any[]): Promise<T>

Parameters

methodName

string

The name of the server method to invoke.

args

any[]

The arguments used to invoke the server method.

Returns

Promise<T>

A Promise that resolves with the result of the server method (if any), or rejects with an error.

off(string)

Removes all handlers for the specified hub method.

function off(methodName: string)

Parameters

methodName

string

The name of the method to remove handlers for.

off(string, (args: any[]) => void)

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on(string, (args: any[]) => void). Passing a different instance (even if the function body is the same) will not remove the handler.

function off(methodName: string, method: (args: any[]) => void)

Parameters

methodName

string

The name of the method to remove handlers for.

method

(args: any[]) => void

The handler to remove. This must be the same Function instance as the one passed to on(string, (args: any[]) => void).

on(string, (args: any[]) => void)

Registers a handler that will be invoked when the hub method with the specified method name is invoked.

function on(methodName: string, newMethod: (args: any[]) => void)

Parameters

methodName

string

The name of the hub method to define.

newMethod

(args: any[]) => void

The handler that will be raised when the hub method is invoked.

onclose((error?: Error) => void)

Registers a handler that will be invoked when the connection is closed.

function onclose(callback: (error?: Error) => void)

Parameters

callback

(error?: Error) => void

The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).

onreconnected((connectionId?: string) => void)

Registers a handler that will be invoked when the connection successfully reconnects.

function onreconnected(callback: (connectionId?: string) => void)

Parameters

callback

(connectionId?: string) => void

The handler that will be invoked when the connection successfully reconnects.

onreconnecting((error?: Error) => void)

Registers a handler that will be invoked when the connection starts reconnecting.

function onreconnecting(callback: (error?: Error) => void)

Parameters

callback

(error?: Error) => void

The handler that will be invoked when the connection starts reconnecting. Optionally receives a single argument containing the error that caused the connection to start reconnecting (if any).

send(string, any[])

Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still be processing the invocation.

function send(methodName: string, args: any[]): Promise<void>

Parameters

methodName

string

The name of the server method to invoke.

args

any[]

The arguments used to invoke the server method.

Returns

Promise<void>

A Promise that resolves when the invocation has been successfully sent, or rejects with an error.

start()

Starts the connection.

function start(): Promise<void>

Returns

Promise<void>

A Promise that resolves when the connection has been successfully established, or rejects with an error.

stop()

Stops the connection.

function stop(): Promise<void>

Returns

Promise<void>

A Promise that resolves when the connection has been successfully terminated, or rejects with an error.

stream<T>(string, any[])

Invokes a streaming hub method on the server using the specified name and arguments.

function stream<T>(methodName: string, args: any[]): IStreamResult<T>

Parameters

methodName

string

The name of the server method to invoke.

args

any[]

The arguments used to invoke the server method.

Returns

An object that yields results from the server as they are received.