info@voip-sip-sdk.com Tel: +36 1 371 0150
Course 1 / Lecture 5

How to develop a softphone in C#

How to send and receive SIP Instant Messages


Download: 05_Instant_Messages.zip

You can send and receive instant messages by using your softphone. This article contains a guide about introducing how to create your own softphone with the availability of sending SIP Instant Messages with the help of Ozeki VoIP SIP SDK.

This example continues to develop the "SIP Registration" example, where you could learn how to register a softphone to a PBX with a SIP account, using Ozeki VoIP SIP SDK.

Introduction

Instant messaging (IM) is a form of real-time direct text-based chatting communication in push mode between two or more people using personal computers or other devices, along with shared clients. The user's text is conveyed over a network, such as the Internet. More advanced instant messaging software clients also allow enhanced modes of communication, such as live voice or video calling and inclusion of links to media.


Figure 1 - SIP instant message sending

Session Initiation Protocol (SIP) is used for establishing a phone line between two VoIP clients using a PBX. In case of an instant messaging system the PBX is not needed, the clients can communicate peer to peer.

Ozeki VoIP SIP SDK has the support for SIP instant messaging between VoIP clients as the following example will show you.

The support for instant message sending can be used by calling some provided methods of Ozeki VoIP SIP SDK. The message should be typed in and the sending method should be called.

The following program code uses the background support of Ozeki VoIP SIP SDK, therefore you will need to download and install the SDK on your computer before starting to use the program code. You will also need to have Visual Studio 2010 (or higher) or compatible IDE and .NET Framework installed on your system, as the program code below is written in C# language.

Solution, written in C#

This guide continues to develop the first example, called "SIP Registration". Please note that, only the softphone's new elements will be introduced here.

As in the previous articles, this guide is using two classes for the purpose to separate the user interface from the softphone itself: Program.cs, Softphone.cs.

What is Softphone.cs used for?

This class is used to introduce how to declare, define and initialize a softphone, how to handle some of the Ozeki VoIP SIP SDK's events and how to use some of that's functions. In other words, we would like to create a "telephone software", which has the same functions (or much more), as an ordinary mobile (or any other) phone. In the Program.cs class we will use this class to create a new softphone, so we can use the functions, we can listen to the events placed here.

Objects and variables

This application's Softphone class needs only two objects and one variable:

private ISoftPhone softphone; // softphone object
private IPhoneLine phoneLine; // phoneline object

public string recipient; // the recipient's number as string

As you can see, this class is using a ISoftPhone and an IPhoneLine object. You can initialize them, as you did in the first example.
There are two possible ways to send instant messages to an other party:

  • through the IPhoneLine object, with the object's SendOutofDialogInstantMessage() method. In this case, you don't need to establish a call with the other party. This example introduces this case.
  • through the IPhoneCall object, with the SendInstantMessage() method. In this case, you have to establish a call before you try to send an instant message.

This example doesn't need any IPhoneCall or Microphone, Speaker, MediaConnector etc. MediaHandler objects, since it sends messages without establishing a call and doesn't need the devices, which an audio call would.

How to send Instant Messages?

To send an instant message, you need to set the IPhoneLine's object's (called phoneLine in this example) with an InstantMessage parameter:

  • message: an InstantMessage object
public void SendMessage(InstantMessage message)
	{
	    phoneLine.InstantMessaging.SendMessage(message);
	}

This method will receive the message string's value from the user, and will send the given message to the recipient, defined by the recipient variable.

How to receive Instant Messages?

To receive instant messages through the phoneLine object, the application needs to be subscribed to the MessageReceived event of the phoneLine:

phoneLine.InstantMessaging.MessageReceived += phoneLine_InstantMessageReceived;

The phoneLine_InstantMessageReceived method notifies the application if there is an incoming instant message through an event, called IncomingMessage in this example.

Please note that, if you are editing the source code of the first example, called "SIP Registration", since there is no IPhoneCall object being used in this example, you can remove that and all of its dependencies from the source code (for example the handling of the call's states).

What is Program.cs used for?

This class will introduce the usage of a softphone object, handles the console events, interacts with the user, and uses the opportunities provided by the Softphone class. In this example, the softphone can send and receive Instant Messages. We are handling everything with separated methods; these methods are communicating with each other, and making the source code more understandable and reusable.

What objects and variables are being used?

There is only one new variable: messageToSend, to store the message which is being sent, as String:

static InstantMessage messageToSend;

How to initialize the softphone?

The first step is to initialize the softphone, we have created. To do this, we need to call an initializer method. We also need to subscribe to the softphone's events. If you are using the SIP Registration example's source code, to do this, you need to add the following line:

mySoftphone.IncomingMessage += mySoftphone_IncomingMessage;

As you can see, the application is subscribing to the IncomingMessage event of the Softphone class, and is setting the declared variable to be empty.

How to get the phone number of the recipient?

The application is using a method, called GetRecipient() for the purpose to ask the user about the recipient's number, and to store that in the previously declared recipient variable of the Softphone object, called mySoftphone in this example. This method is being called when the registration was successful (or no registration was needed).

How to get message from the user to be sent?

There is a method, called MessageToSend in this example, which is asking the user for messages to be sent, and sends those to the recipient, until the user closes the application. This method is using the mySoftphone object's SendMessage() method (see above), with the user's message as parameter:

var content = GetMessage();
messageToSend = new InstantMessage(mySoftphone.recipient, content);
mySoftphone.SendMessage(messageToSend);

The MessageToSend() method is being called after the setting of the recipient's number.

How to receive incoming instant messages?

To receive an instant message, the softphone is already subscribed to the necessary event (see above), which calls the mySoftphone_IncomingMessage() method, which displays the received message's originator and the message itself:

Console.WriteLine("\nMessage received from {0}: {1}", instantMessage.Sender, instantMessage.Content);

Conclusion

After reading this article, you must be familiar with instant message sending and receiving without making a call.

If you have any questions or need assistance, please contact us at info@voip-sip-sdk.com

You can select a suitable Ozeki VoIP SIP SDK license for your project on Pricing and licensing information page

Related Pages

Ozeki Cookie Policy
Ozeki Informatics Ltd uses cookies to provide you the best experience on this website. The further use of the website will be considered as an agreement to the use of cookies. For more information read this website.

Cookies are enabled You are browsing the optimized version of this website. For more information read this website.