Class: DuplexTypedMessageSender

DuplexTypedMessageSender

new DuplexTypedMessageSender()

Sends request messages and receives response messages of a specific type.
It uses JsonSerializer to serialize/deserialize messages.
Source:
Example
// Create the duplex output channel.
var anOutputChannel = new WebSocketDuplexOutputChannel("ws://127.0.0.1:8890/MyService/", null);

// Create DuplexTypedMessageSender.
var aSender = new DuplexTypedMessageSender();

// Subscribe to receive response messages.
aSender.onResponseReceived = onResponseReceived;

// Attach output channel and be able to send messages and receive responses.
aSender.attachDuplexOutputChannel(anOutputChannel);

...

// Message which shall be sent. 
function MessageData(name, number) {
    this.Name = name;
    this.Number = number;
};

// Send a message.
var aMessage = new MessageData("Hello World.", 123);
aSender.sendRequestMessage(aMessage);

...

// Detach output channel and stop listening to responses.
aSender.detachDuplexOutputChannel();

...

// Event handler processing received response messages.
function onResponseReceived(typedResponseReceivedEventArgs) {

    // Note: aMessage is already deserialized message.
    var aMessage = typedResponseReceivedEventArgs.ResponseMessage;
    ...
}

Extends

Methods

_onResponseMessageReceived()

The event is invoked when the response message is received. This event handler method is supposed to be overridden by derived classes.
Inherited From:
Source:

attachDuplexOutputChannel(outputChannel)

Attaches the duplex output channel and opens the connection for sending request messages and receiving response messages.
Please notice when the call returns from this method the connection does not have to be open yet. It can be still in the state opening. Use onConnectionOpened eent to detect when the connetion is open.
Parameters:
Name Type Description
outputChannel WebSocketDuplexOutputChannel
Inherited From:
Source:
Throws:
Throws an error if attaching fails.

detachDuplexOutputChannel()

Detaches the duplex output channel and stops listening to response messages.
Inherited From:
Source:

getAttachedDuplexOutputChannel()

Returns attached duplex output channel.
Inherited From:
Source:

isDuplexOutputChannelAttached()

Returns true if the duplex output channel is attached.
Please notice when the channel is attached it does not have to be connected. E.g. once the output channel was attached and the connection was open and then the connection was broken or closed by input channel.
Inherited From:
Source:

onConnectionClosed()

The event which can be subscribed to receive the notification when the connection was closed.
Inherited From:
Source:
Example
// Set your handler to receive close connection notification.
aSender.onConnectionClosed = yourOnConnectionClosed;

onConnectionOpened()

The event which can be subscribed to receive the notification when the connection is open.
Inherited From:
Source:
Example
// Set your handler to receive open connection notification. 
aSender.onConnectionOpened = yourOnConnectionOpened;

onResponseReceived(responseMessage)

The event which can be subscribed to receive response messages.
Parameters:
Name Type Description
responseMessage TypedResponseReceivedEventArgs received response message.
Source:
Example
// Create DuplexTypedMessageSender.
var aSender = new DuplexTypedMessageSender();

// Subscribe to receive response messages.
aSender.onResponseReceived = onResponseReceived;

...

// Event handler processing received response messages.
function onResponseReceived(typedResponseReceivedEventArgs) {

    // Note: aMessage is already deserialized message.
    var aMessage = typedResponseReceivedEventArgs.ResponseMessage;
    ...
}

sendRequestMessage(message)

Serializes the given message and sends it.
Parameters:
Name Type Description
message object that shall be serialized and sent.
Source:
Throws:
Throws error if sending fails.