javascriptc#socketswebsocket

Sending binary data by socket between C# and Js


I'm trying to send binary data between server (C#) application and client (js-application -- made by WebSocket). Connection between server and client is established and handshake is OK. Messages from client are receiving by server, but when I'm trying to send binary data to client, the event "onmessage" doesn't work. This is fragments of my C# code. Sending binary data in "sendFile" function.

    class Listener
    {
        private IPAddress ip;
        private int port;
        private TcpListener server;
        private TcpClient client;
        private NetworkStream stream;
        private bool isSuccHandshaked;

        public Listener()
        {
            ip = IPAddress.Loopback;
            port = 8080;
            server = new TcpListener(ip, port);
            isSuccHandshaked = false;
        }

        private void makeHandshake()
        {
            //...   
        }

        private String decodeMessage(Byte[] bytes)
        {
           //...
        }

        private void sendFile()
        {
            Byte[] dataToSend = File.ReadAllBytes("test.txt");
            stream.Write(datatosend, 0, datatosend.Length);
            stream.Flush();
        }

        public void startListen()
        {
            server.Start();
            Console.WriteLine("Server has started on {0}. Port: {1}. {2}Waiting for a connection...", ip, port, Environment.NewLine);

            client = server.AcceptTcpClient();
            Console.WriteLine("A client connected.");

            stream = client.GetStream();
            while (!isSuccHandshaked)
            {
                makeHandshake();
            }

            while (true)
            {
                if (client.Available > 0)
                {
                    Byte[] bytes = new Byte[client.Available];
                    stream.Read(bytes, 0, bytes.Length);
                    String message = decodeMessage(bytes);
                    sendFile();
                }
             }
        }
    }
}

and js-code:

    var address = 'ws://localhost:8080';
    var socket = new WebSocket( address );
    socket.binaryType = "arraybuffer";
    socket.onopen = function () {
        alert( 'handshake successfully established. May send data now...' );
        socket.send( "Aff" );
    };
    socket.onclose = function () {
        alert( 'connection closed' );
    };
    socket.onmessage = function ( evt ) {
        console.log( "Receive message!" );
        console.log( "Got ws message: " + evt.data );
    }

Maybe there are some peculiar properties in receiving data with WebSocket protocol? Which useful approaches to send binary data from C# code to js you can recommend?


Solution

  • A websocket is not a raw TCP socket. It uses HTTP negotiation and its own framing protocol you have to comply with. If you are interested in writing your own server in C# take a look at this.

    However if you only want to use them, you can either use the default Microsoft implementation with IIS, or you can use one of the many standalone websockets components. I maintain one named WebSocketListener.