socketIO和.NET客户端的奇怪行为

时间:2018-12-16 09:51:03

标签: c# websocket uwp socket.io

我在节点上使用socketIO并将其与前端连接,它们工作正常 现在我想进一步推动事情

我希望我的.net客户端(UWP)与socketIO通信 但是确实发生了一些奇怪的事情

首先,我进行了测试,并且运行正常,第二次运行客户端 当我仅发送一条消息时,它开始发送两次或更多消息。而且我意识到,如果我先打开.net客户端,然后再打开服务器,则不会发生这种情况,但是当我关闭客户端(在服务器仍在运行时)时,就会开始发生此错误。

public sealed partial class StuffDetail : Page
 {
    Socket socket = IO.Socket("ws://localhost:8888");
    ObservableCollection<SocketMessage> InCommingData { get; set; } = new ObservableCollection<SocketMessage>();
    public StuffDetail()
    {
        this.InitializeComponent();
        DisconnectState();
        SetSocket();
        ChatWindow.ItemsSource = InCommingData;
    }

    private void DisconnectState()
    {
        ReceivedText.Text = "Not Connect";
    }



    private void SetSocket()
    {
        socket.Connect();
        ReceivedText.Text = "Connect Completed";

        socket.On("chat", async (data) =>
        {

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                var IncomeMess = JsonConvert.DeserializeObject<SocketMessage>(data.ToString());
                InCommingData.Add(IncomeMess);
            });

        });


    }



    private void Send(object sender, RoutedEventArgs e)
    {
        var message = new SocketMessage()
        {
            message = MessTextBox.Text,
            handle = "From Beta User"
        };
        socket.Emit("chat", JsonConvert.SerializeObject(message));
    }
}

0 个答案:

没有答案