Skip to content

Commit

Permalink
Merge pull request #4 from Lucas8448/Lucas
Browse files Browse the repository at this point in the history
Fix errors
  • Loading branch information
Lucas8448 authored Jan 5, 2024
2 parents c9cd10f + ead8d2a commit 60e46ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions src/Client.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import Peer from 'peerjs';

const Client = () => {
Expand All @@ -8,6 +8,12 @@ const Client = () => {
const [peer, setPeer] = useState(null);
const [conn, setConn] = useState(null);

const connectToHost = useCallback(() => {
if (!peer) return;
const connection = peer.connect("PostIt" + hostId);
setupConnection(connection);
}, [peer, hostId]);

useEffect(() => {
const newPeer = new Peer();
newPeer.on('open', id => {
Expand All @@ -27,13 +33,7 @@ const Client = () => {
}, 5000);

return () => clearInterval(intervalId); // Cleanup on unmount
}, [peer, conn]);

const connectToHost = () => {
if (!peer) return;
const connection = peer.connect("PostIt" + hostId);
setupConnection(connection);
};
}, [peer, conn, connectToHost]);

const setupConnection = (connection) => {
setConn(connection);
Expand Down Expand Up @@ -90,4 +90,4 @@ const Client = () => {
);
};

export default Client;
export default Client;
36 changes: 18 additions & 18 deletions src/Host.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import Peer from 'peerjs';

const Host = () => {
Expand All @@ -14,6 +14,22 @@ const Host = () => {
}
return numbers;
}

const setupConnection = useCallback((connection) => {
setConn(connection);
connection.on('open', () => {
alert('A client has connected!');
});

connection.on('data', data => {
handleData(data)
});

connection.on('error', err => {
console.error('Connection error:', err);
setConn(null);
});
}, []);

useEffect(() => {
const newPeer = new Peer("PostIt" + generateID());
Expand All @@ -34,23 +50,7 @@ const Host = () => {
}, 5000); // Check every 5 seconds

return () => clearInterval(intervalId); // Cleanup on unmount
}, [conn]);

const setupConnection = (connection) => {
setConn(connection);
connection.on('open', () => {
alert('A client has connected!');
});

connection.on('data', data => {
handleData(data)
});

connection.on('error', err => {
console.error('Connection error:', err);
setConn(null);
});
};
}, [conn, setupConnection]);

const sendData = () => {
if (conn && conn.open) {
Expand Down

0 comments on commit 60e46ba

Please sign in to comment.