简体   繁体   中英

can't send message from javascript to c# using fleck

So i have a problem. I want to send some message from javascript to my c# server using fleck. I start a server in my app like thease:

    var allSockets = new List<IWebSocketConnection>();
        var server = new WebSocketServer("ws://localhost:8081");
        server.Start(socket =>
        {
            socket.OnOpen = () => allSockets.Add(socket);
            socket.OnClose = () => allSockets.Remove(socket);
            socket.OnMessage = message =>
            {
                foreach (var s in allSockets.ToList())
                    s.Send(message);
            };
        });

And on my javascript side i have:

   function sendPodatke(){
        document.write('send!');
        socket.send("javascript");
        document.write('aftersend!');
    }

    function connect(){
    document.write('connect!');
      socket = new WebSocket('ws://localhost:8081');
      socket.onmessage = function(mess) {
        animateCharacter(mess.data);
      };

    };

    connect();
    sendPodatke();

So i get write connect!, send! but not afterSend. IOn my c# server i see that connection is done but i newer get a onMessage call..

Any ideas?

thank u all in advance!

A websocket handshake is an asynchronous operation. You can register an onopen callback in your client and should only send data from client to server once this runs. See the websocket API docs for details.

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