在 socket.io 中连接 2 个不同的应用程序(REACT NATIVE)
connect 2 different application in socket.io (REACT NATIVE)
大家好,我无法从 application a
获取数据到 application b
我的 application a
useEffect(() => {
setSocket(io('http://MY_SERVER_IP:3000', {
transports: ['websocket'],
jsonp:false,
forceNew: true,
}));
},[]);
const SendData = () => {
socket.emit("user app info", dataInfo);
}
现在在我的 server
上是这个
io.on("connection", socket => {
console.log("a user connected :D");
//User info's
socket.on("user app info", dataInfo => {
// console.log(msg);
io.emit("user app info", dataInfo);
});
//Driver info's
socket.on("driver app info", msg => {
io.emit("driver app info", msg);
});
});
现在我需要将它发送到我的 application b
这是我的做法,但现在可以工作了
useEffect(() => {
setSocket(io('http://MY_SERVER_IP:3000', {
transports: ['websocket'],
jsonp: false,
forceNew: true,
}));
}, []);
const onAccept = (newOrder) => {
socket.on("user app info", dataInfo => console.log(dataInfo));
}
但它根本不会显示任何日志。有人可以启发我吗?谢谢
在 React Native 上使用 WebSocket API
第 1 步 -
为 WSS 连接创建引用
const ws = React.useRef(null)
第 2 步 -
初始化网络套接字
React.useEffect(() => {
ws.current = new WebSocket('URL to WS')
ws.current.onopen = () => console.log('Connected')
ws.current.onclose = () => console.log('Disconnected')
return () => {
ws.current.close()
}
}, [])
第 3 步 -
添加 onMessage 侦听器以接收传入的 WS 数据。
React.useEffect(() => {
if (!ws.current) return
ws.current.onmessage = (val) => {
const response = JSON.parse(val.data)
console.log(response)
}
}, [])
发送一些数据使用
ws.current.send(
JSON.stringify({
...your payload data to server
})
)
这是最小的 WSS 连接代码,剩下的就是您处理接收到的数据并将其发送到服务器的方式。
大家好,我无法从 application a
获取数据到 application b
我的 application a
useEffect(() => {
setSocket(io('http://MY_SERVER_IP:3000', {
transports: ['websocket'],
jsonp:false,
forceNew: true,
}));
},[]);
const SendData = () => {
socket.emit("user app info", dataInfo);
}
现在在我的 server
上是这个
io.on("connection", socket => {
console.log("a user connected :D");
//User info's
socket.on("user app info", dataInfo => {
// console.log(msg);
io.emit("user app info", dataInfo);
});
//Driver info's
socket.on("driver app info", msg => {
io.emit("driver app info", msg);
});
});
现在我需要将它发送到我的 application b
这是我的做法,但现在可以工作了
useEffect(() => {
setSocket(io('http://MY_SERVER_IP:3000', {
transports: ['websocket'],
jsonp: false,
forceNew: true,
}));
}, []);
const onAccept = (newOrder) => {
socket.on("user app info", dataInfo => console.log(dataInfo));
}
但它根本不会显示任何日志。有人可以启发我吗?谢谢
在 React Native 上使用 WebSocket API
第 1 步 -
为 WSS 连接创建引用
const ws = React.useRef(null)
第 2 步 -
初始化网络套接字
React.useEffect(() => {
ws.current = new WebSocket('URL to WS')
ws.current.onopen = () => console.log('Connected')
ws.current.onclose = () => console.log('Disconnected')
return () => {
ws.current.close()
}
}, [])
第 3 步 -
添加 onMessage 侦听器以接收传入的 WS 数据。
React.useEffect(() => {
if (!ws.current) return
ws.current.onmessage = (val) => {
const response = JSON.parse(val.data)
console.log(response)
}
}, [])
发送一些数据使用
ws.current.send(
JSON.stringify({
...your payload data to server
})
)
这是最小的 WSS 连接代码,剩下的就是您处理接收到的数据并将其发送到服务器的方式。