React Native socket.io 客户端连接成功,但两端均未收到发出



我正在尝试在socket-io服务器和iOS应用程序之间进行通信。设备根据两个日志进行连接,但来自客户端或服务器的任何socket.emit()操作都不会触发另一端的相应.on()事件。下面是产生此问题的最小示例。

服务器代码:

from aiohttp import web
import socketio
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
@sio.on('connect', namespace='/mp')
async def connect(sid, environ):
    print("connect ", sid)
@sio.on('chat message', namespace='/mp')
async def message(sid, data):
    print("message ", data)
@sio.on('disconnect', namespace='/mp')
def disconnect(sid):
    print('disconnect ', sid)
if __name__ == '__main__':
    web.run_app(app)

客户端代码(React Native 应用程序(

window.navigator.userAgent = 'ReactNative';
import React from "react"
import openSocket from "socket.io-client"
import { StyleSheet, Text, View, FlatList } from "react-native"
console.ignoredYellowBox = ["Remote debugger"]
import { YellowBox } from "react-native"
YellowBox.ignoreWarnings([
    "Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?"
])
var app = null
var socket = openSocket("http://localhost:8080/mp", {
            jsonp: false,
            reconnection: true,
            reconnectionDelay: 500,
            reconnectionAttempts: Infinity,
            transports: ["websocket"]
        })
export default class App extends React.Component {
    constructor(props) {
        super(props)
        app = this
        socket.on("connect", function() {
            console.log("connected")
            socket.emit("message", "test")
        })
    }
    render() {
        app = this
        return (
            <View style={styles.container}>
                <Text>Open up App.js to start working on your app!</Text>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: "#fff",
        marginLeft:20,
        marginTop: 40,
    }
})

事实证明,问题是我正在向服务器发送事件message,但服务器正在侦听事件chat message(我混淆了事件名称及其功能(。即便如此,服务器到客户端的通信仍然不起作用,所以我想它更多的是调试:P

相关内容

  • 没有找到相关文章