Skip to content

Commit 6cd7645

Browse files
committed
fix: progressing with port
1 parent cf55701 commit 6cd7645

File tree

10 files changed

+1288
-26
lines changed

10 files changed

+1288
-26
lines changed

src/api.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { util } from "./util";
2+
import logger from "./logger";
3+
4+
export class API {
5+
constructor(private readonly _options: any) { }
6+
7+
private _buildUrl(method: string): string {
8+
const protocol = this._options.secure ? "https://" : "http://";
9+
let url =
10+
protocol +
11+
this._options.host +
12+
":" +
13+
this._options.port +
14+
this._options.path +
15+
this._options.key +
16+
"/" +
17+
method;
18+
const queryString = "?ts=" + new Date().getTime() + "" + Math.random();
19+
url += queryString;
20+
21+
return url;
22+
}
23+
24+
/** Get a unique ID from the server via XHR and initialize with it. */
25+
async retrieveId(): Promise<string> {
26+
const url = this._buildUrl("id");
27+
28+
try {
29+
const response = await fetch(url);
30+
31+
if (response.status !== 200) {
32+
throw new Error(`Error. Status:${response.status}`);
33+
}
34+
35+
return response.text();
36+
} catch (error) {
37+
logger.error("Error retrieving ID", error);
38+
39+
let pathError = "";
40+
41+
if (
42+
this._options.path === "/" &&
43+
this._options.host !== util.CLOUD_HOST
44+
) {
45+
pathError =
46+
" If you passed in a `path` to your self-hosted PeerServer, " +
47+
"you'll also need to pass in that same path when creating a new " +
48+
"Peer.";
49+
}
50+
51+
throw new Error("Could not get an ID from the server." + pathError);
52+
}
53+
}
54+
55+
/** @deprecated */
56+
async listAllPeers(): Promise<any[]> {
57+
const url = this._buildUrl("peers");
58+
59+
try {
60+
const response = await fetch(url);
61+
62+
if (response.status !== 200) {
63+
if (response.status === 401) {
64+
let helpfulError = "";
65+
66+
if (this._options.host === util.CLOUD_HOST) {
67+
helpfulError =
68+
"It looks like you're using the cloud server. You can email " +
69+
"team@peerjs.com to enable peer listing for your API key.";
70+
} else {
71+
helpfulError =
72+
"You need to enable `allow_discovery` on your self-hosted " +
73+
"PeerServer to use this feature.";
74+
}
75+
76+
throw new Error("It doesn't look like you have permission to list peers IDs. " +
77+
helpfulError);
78+
}
79+
80+
throw new Error(`Error. Status:${response.status}`);
81+
}
82+
83+
return response.json();
84+
} catch (error) {
85+
logger.error("Error retrieving list peers", error);
86+
87+
throw new Error("Could not get list peers from the server." + error);
88+
}
89+
}
90+
}

src/baseconnection.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Base abstractions for peer to peer connections."""
2+
from pyee import AsyncIOEventEmitter
3+
from ..peer import Peer
4+
from servermessage import ServerMessage
5+
from enums import ConnectionType
6+
from abc import abstractmethod
7+
from aiortc import RTCPeerConnection
8+
9+
10+
class BaseConnection(AsyncIOEventEmitter):
11+
"""Base abstract class for peer to peer connections."""
12+
13+
@abstractmethod
14+
def type() -> ConnectionType:
15+
"""Return connection type."""
16+
17+
@property
18+
def open(self):
19+
"""Return True if this connection is open."""
20+
return self._open
21+
22+
def __init__(
23+
self,
24+
peer: str = None,
25+
provider: Peer = None,
26+
options: any = None
27+
):
28+
"""Create connection construct."""
29+
super()
30+
self.metadata = options.metadata
31+
self._open = False
32+
self.connectionId: str = None
33+
self.peerConnection: RTCPeerConnection = None
34+
35+
@abstractmethod
36+
def close(self) -> None:
37+
"""Close this connection."""
38+
39+
@abstractmethod
40+
def handleMessage(self, message: ServerMessage) -> None:
41+
"""Handle incoming message."""

0 commit comments

Comments
 (0)