【问题标题】:Push notification via C# socket通过 C# 套接字推送通知
【发布时间】:2018-11-19 06:09:23
【问题描述】:

我想通过 C# 套接字服务器向 React Native 发送推送通知。在反应本机方面,我使用 socket.io。 React Native 连接到服务器,但永远不会收到消息。

React Native 代码:

socket = io.connect('xx.xx.xx.xx:2201');

    socket.on((messages) => {
      console.log("response" + JSON.stringify(messages));   
});

服务器代码:

 public static void StartListening()
    {
        byte[] bytes = new Byte[1024];  
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        /
        Socket listener = new Socket(ipAddress.AddressFamily,SocketType.Stream, ProtocolType.Tcp);
        listener.Bind(localEndPoint);
        listener.Listen(10);
            while (true)
            {
                Socket handler = listener.Accept();
                data = null;
            int bytesRec = handler.Receive(bytes);
            data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
            Console.WriteLine("Receieved message and sent: " + data);
            byte[] msg1 = Encoding.ASCII.GetBytes("jjjjjj");
            handler.Send(msg1);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

【问题讨论】:

    标签: c# react-native socket.io


    【解决方案1】:

    也许这对你有帮助。

    import SocketIOClient from 'socket.io-client';
    this.socket = SocketIOClient('xx.xx.xx.xx:2201', {
            transports: ['websocket']
        });
     this.socket.on('message', (message) => { // you can send 'message' emit from server side and than get the on by using 'message' keyword
         console.log(message)
     });
    

    【讨论】:

    • 我无法从服务器发出,因为服务器是用 c# 编写的。