套接字客户端和服务器未连接

时间:2021-05-12 01:39:41

标签: react-native websocket socket.io io peerjs

我正在尝试将我的 socket.io 和 peerjs 连接到一个 react-native 视频聊天应用......这是我的代码如下

客户端

export const API_URI = 'https://server.herokuapp.com'


// Peer Config

const peerServer = new Peer(undefined, {
    host: 'server.herokuapp.com',
    config : {
        iceServers: [{"url": "stun:stun.l.google.com:19302"}]
    },
    secure: true,
    port: 443,
    path: '/mypeer'
})

peerServer.on('error', console.log)
// Socket config

export const socket = IO(`${API_URI}`, {
    forceNew: true
})

socket.on('connection', () => console.log('Connection'))

export const joinRoom = (stream) => async (dispatch) => {
    console.log('joinRoom')
    const roomID = 'kkddjjjff'
    //set my own stream
    dispatch({type: MY_STREAM, payload: stream})

    //Open a connection to our server
    peerServer.on('open', (userId) => {
        console.log("peer.open")
        socket.emit('join-room', {userId, roomID})
    })

    socket.on('user-connected', (userId) => {
        console.log('user connected')
        connectToNewUser(userId, stream, dispatch)
    })

    // Receive a call
    peerServer.on('call', (call) => {
        call.answer(stream)

        //stream back the call

        call.on('stream', (stream) => {
            dispatch({type: ADD_STREAM, payload: stream})
        })
    })
};

我的服务器端代码

app.use(express.json())

const customGenerationFunction = () =>     (Math.random().toString(36) + "0000000000000000000").substr(2, 16)

const peerServer = ExpressPeerServer(server, {
    debug: true,
    path: "/",
    genderateClientId: customGenerationFunction
})

app.use("/mypeer", peerServer)
console.log('videocall')
io.on('connection', (socket) => {
    console.log('connected')
    socket.on('join-room', function(roomID, userId) {
        console.log('joined rooom' + roomID)
        socket.join(roomID)
        socket.to(roomID).emit('user-connected', userId)
    })
})

const port = process.env.PORT || 5000

问题

目前,服务器端控制台中唯一记录的是“已连接”,这表明 io.on('connection') 有效。当我启动加入房间功能时,它不会记录任何内容,而应该记录“加入房间xxx”。

我的客户端控制台没有记录任何内容,而当 socket.on('connection') 时它应该记录“Connection”

服务器使用socket.io@2.3.0 客户端使用 socket.io-client@2.3.1

请帮忙,如果您需要更多详细信息,请告诉我

0 个答案:

没有答案