WebSocket connection to ...ws://localhost:9000/.. failed: Insufficient resources #1047
Replies: 2 comments 1 reply
-
Transferred to the client repository. Just a wild guess, as there’s not enough information to reproduce this: |
Beta Was this translation helpful? Give feedback.
-
Hey @jonasgloning - thanks for getting back to me. You are correct, I definitely do that. I'm assigning a listener for 'data' but I'm also removing it when the component is removed. See the snippets below. import Peer, { DataConnection } from 'peerjs';
import { createContext, ReactNode, useEffect, useState } from 'react';
// Connection Statis
enum ConnectionStatus {
CLOSED = 'CLOSED',
CONNECTING = 'CONNECTING',
OPEN = 'OPEN',
}
// This is the interface for the context
interface PeerConnectionsContextType {
id: string;
peerClient: Peer;
peerConnections: {[key: string]: DataConnection};
peerConnectionStatus: {[key: string]: ConnectionStatus };
addPeer: (peerId: string) => void;
sendMessage: (peerId: string, message: any) => void;
setPeerDataHandler: (data: any) => void;
}
// Instantiate context for tracking peers
const PeerConnectionsContext = createContext<PeerConnectionsContextType>({
id: '',
peerClient: {} as any,
peerConnections: {},
peerConnectionStatus: {},
addPeer: (_: string) => {},
sendMessage: (_: string, __: any) => {},
setPeerDataHandler: () => (_: any) => {},
});
const PeerConnections: React.FC<{ children: ReactNode }> = ({ children }) => {
// Setup current users peer client
const [ peerClient ] = useState<Peer>(new Peer({host: "/", port: 9000})) // This is a local dev server;
const [ id, setId ] = useState<string>('UNKNOWN');
// Define storage for connections to peers
const [ peerConnections, setPeerConnections ] = useState<{[key: string]: DataConnection}>({});
const [ peerConnectionStatus, setPeerConnectionStatus ] = useState<{[key: string]: ConnectionStatus}>({});
// Define the callback function for receiving messages
const [ peerDataHandler, setPeerDataHandler ] = useState<(data: any) => void>(() => (_: any) => {
console.log("No peer data handler has been set.")
})
// Set ID when it becomes available, listen for INCOMING connections
useEffect(() => {
peerClient.on('open', (id) => {
setId(id);
});
peerClient.on('connection', (connection) => {
setPeerConnections({ ...peerConnections, [connection.peer]: connection });
setPeerConnectionStatus({ ...peerConnectionStatus, [connection.peer]: ConnectionStatus.OPEN });
});
() => {
peerClient.removeAllListeners('connection');
peerClient.removeAllListeners('open');
}
}, []);
useEffect(() => {
// Listen for messages from peers
Object.values(peerConnections).forEach((connection) => {
connection.on('data', peerDataHandler);
})
return () => {
Object.values(peerConnections).forEach((connection) => {
connection.removeAllListeners('data');
})
}
}, [peerConnections, peerDataHandler])
// Add a peer
const addPeer = (peerId: string) => {
// If the client is already a peer, log an error
if (peerConnections[peerId]) {
console.log("That peer has already been added.")
return
}
// Connect to the client using the provided ID
const connection = peerClient.connect(peerId, { reliable: true });
setPeerConnections({ ...peerConnections, [peerId]: connection });
setPeerConnectionStatus({ ...peerConnectionStatus, [peerId]: ConnectionStatus.CONNECTING })
connection.on('open', () => {
setPeerConnections({ ...peerConnections, [peerId]: connection });
setPeerConnectionStatus({ ...peerConnectionStatus, [peerId]: ConnectionStatus.OPEN })
})
connection.on('close', () => {
const newPeerConnections = { ...peerConnections };
delete newPeerConnections[connection.peer];
setPeerConnections(newPeerConnections);
setPeerConnectionStatus({ ...peerConnectionStatus, [connection.peer]: ConnectionStatus.CLOSED });
});
};
const sendMessage = (peerId: string, message: any) => {
peerConnections[peerId].send(message);
}
return (
<PeerConnectionsContext.Provider value={{ peerClient, id, peerConnections, peerConnectionStatus, addPeer, sendMessage, setPeerDataHandler }}>
{children}
</PeerConnectionsContext.Provider>
);
};
export { PeerConnections, PeerConnectionsContext }; |
Beta Was this translation helpful? Give feedback.
-
I'm having an issue:
Beta Was this translation helpful? Give feedback.
All reactions