简体   繁体   中英

How to establish a socket connection between a Play 2 server and a C# application?

I have to establish a connection between a server written in Java using Play framework 2 and a client desktop application written in C#. The nature of the system is such that a lot of messages should be pushed from server to client, so using sockets seemed to be the natural approach, especially since they are supported by Play (namely, WebSockets).

However, the support of WebSockets by C# is kind of lacking. I managed to use the System.Net.WebSockets.ClientWebSocket class to connect to my server, but this class is very low-leveled, working with buffers of type ArraySegment<byte> is nowhere close to the js-implementation of WebSockets, where i can just receive a string of variable length. This class seems to be the last resort for me, in case i don't find a better solution.

There's also the Alchemy Websockets library, but i just can't get it to work. I start my server, check that it is working with the http://www.websocket.org/echo.html page and then start a console client, but it does absolutely nothing, no response, the server does not notice anything that tries to connect. Here is the C# code i use for it:

private static void Main()
{
    var aClient = new Alchemy.WebSocketClient("ws://localhost:9000/wstest")
    {
        OnReceive = context => Console.WriteLine("receive"),
        OnConnected = context => Console.WriteLine("connect"),
        OnSend = context => Console.WriteLine("send"),
        OnDisconnect = context => Console.WriteLine("dissend")
    };
    aClient.Connect();
    aClient.Send("HI!");
    Console.ReadLine();
}

So, my question is: are there any implementations of WebSocket clients or probably libraries built on top of ClientWebSocket that i haven't found yet?

Or perhaps there is some way to use simple sockets (not WebSockets) with Play framework 2? I would be glad to get any help.

事实证明,第一个websocket4net库以某种方式被忽略了,效果很好并且易于使用,因此使用该库是我的问题的答案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM