0

First of all lets discuss my case below: I have three part comes and play in my case: 1- Client HTML page 2- ASP.NET API that hosted on windows 2008 Web server 3- Black-Box TCP Socket Server (Hosted on internal zone and does not accessible from out side world) which will receive requests and send back with the response.

The HTML page sends an AJAX call to ASP.NET API service , After that the API service will open a connection to internal TCP Socket server (Black Box).

I would like to make the socket connection opened once a day . Since I have a lot of AJAX calls goes and forth to TCP server. I don't need to open a Socket connection for each request.

I already try to use asynchronous socket example using link below : https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx

Any body try this case before , I need the best and easy way to do that ? Any suggestions I will be appreciated ..

  • I add some details above. – Faris Rjoub Aug 02 '15 at 12:52
  • -1 "I would like to make the socket connection opened once a day . Since I have a lot of AJAX calls goes and forth to TCP server. I don't need to open a Socket connection for each request." It sounds like you have absolutely no idea about the http protocol. There are two technologies you can use, [HTTP Persistent Connectio](https://en.wikipedia.org/wiki/HTTP_persistent_connection) or [Websockets](https://en.wikipedia.org/wiki/WebSocket). Both of which is basically the name of your question title, showing you haven't even googled for an answer. – Aron Aug 02 '15 at 14:06
  • However IIS already supports HTTP Persistent Connections. As does most of the modern day browsers. Which suggests to me you are guilty of premature optimisation. – Aron Aug 02 '15 at 14:09
  • My case is related to TCP channel between ASP.NET API and with TCP Socket Server which is hosted on different server .(Server Side Layers). I need to keep the TCP socket connection open all the time , using singleton class or any other techniques that may help. – Faris Rjoub Aug 02 '15 at 19:51
  • And as Database connection pooling method , I need the same idea for TCP Socket connection , Is there a way to manage the TCP connections to be cashed or pools for best performance. – Faris Rjoub Aug 02 '15 at 20:15
  • What you are talking about is very confusing. AJAX is a HTTP technology/terminology, whilst you seem to be talking about raw TCP/IP. – Aron Aug 03 '15 at 10:31

1 Answers1

2

quoting this SO

Well, REST by design is stateless. By adding session (or anything else of that kind) you are making it stateful and defeating any purpose of having a RESTful API.

The whole idea of RESTful service is that every resource is uniquely addressable using a universal syntax for use in hypermedia links and each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP".

So, your web service really shouldn't keep the socket (or any state) data.

If you still think it's vital to use a single socket, her is what I would do:

Implement the SingleSocket class (with corresponding interface). That class would store a single instance of the socket once it's been created (Singleton pattern)

Then, inject the SingleSocket class to you controller through dependency injection framework and use it's methods internally.

This way you can abstract your implementation detail (single open socket), while preserving control's responsibility (communication with TCP server)

Here is a link to dependency injection tutorial.

Here is a pseudo code for dependency injection as I see it in your case:

interface ISocket : IDisposable
{
    void Init();
    void Send();
    void Dispose();
}

public class Mycontroller
{
    private readonly ISocket _singletonSocket;
    public Mycontroller(ISocket singletonSocket)
    {
        _singletonSocket = singletonSocket;
    }

    public void Init(){
        this._singletonSocket.Init();
    }

    public void Send()
    {
        this._singletonSocket.Send(); // handles the whole send/recieve logic
    }

    public void Dispose()
    {
        this._singletonSocket.Dispose();
    }
}

Another thing to note:

I found this socket.io js library while answering your question.

I'd suggest looking into it. Maybe it'll suit your needs better

Community
  • 1
  • 1
DanielS
  • 744
  • 6
  • 13
  • In my case, I have heavy calls for TCP Socket servicce , So I need that connection open all the time ,, Use SingleSocket classs using Singleton patter is ok for me ,But how to make inject that class later inside 'Controller' ,, Any sample code please. – Faris Rjoub Aug 01 '15 at 13:19
  • @Faris, look at my updated answer – DanielS Aug 01 '15 at 14:10
  • Thanks for your help, I will try , – Faris Rjoub Aug 01 '15 at 19:46
  • Another minor issue , Is this singleton object will be only one for all IIS Worker process defined?? Or it will be multi instances for each Worker Process. – Faris Rjoub Aug 02 '15 at 22:08