c# - 通过 C# 套接字推送通知
问题描述
我想通过 C# 套接字服务器向 React Native 发送推送通知。在反应本机方面,我使用 socket.io。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();
}
解决方案
也许这对你有帮助。
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)
});
推荐阅读
- python - 无头 Python Selenium 中的表单提交
- python - 如何将字典中的值附加到列表中?
- excel - AdvancedFilter 函数的语法
- css - 我的引导导航栏在我的 html 中不起作用?
- java - java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为 com.google.gson.internal.LinkedTreeMap
- php - 在 laravel 的表单中使用 post 方法时卡在同一页面
- r - mcp2matrix(模型,linfct = linfct)中的错误:“整数”类的变量“方法”是/不包含在“模型”中
- javascript - 为什么当我尝试将它嵌入到网站时,Wordpress 看不到我的 js 函数?
- c# - 如何制作字典
来自列表 - c++ - Arduino 将字符串复制到 char * 并使用外部函数