@voicemaster/core 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -16,6 +16,7 @@ declare class VoiceClient {
16
16
  private userId;
17
17
  private iceServers;
18
18
  private isConnectedFlag;
19
+ private targetUserId;
19
20
  constructor(config: VoiceClientConfig);
20
21
  on(event: string, callback: Function): void;
21
22
  private emit;
package/dist/index.d.ts CHANGED
@@ -16,6 +16,7 @@ declare class VoiceClient {
16
16
  private userId;
17
17
  private iceServers;
18
18
  private isConnectedFlag;
19
+ private targetUserId;
19
20
  constructor(config: VoiceClientConfig);
20
21
  on(event: string, callback: Function): void;
21
22
  private emit;
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ var VoiceClient = class {
33
33
  this.remoteStream = null;
34
34
  this.eventHandlers = /* @__PURE__ */ new Map();
35
35
  this.isConnectedFlag = false;
36
+ this.targetUserId = null;
36
37
  this.signalingUrl = config.signalingUrl;
37
38
  this.roomId = config.roomId;
38
39
  this.userId = config.userId;
@@ -82,10 +83,6 @@ var VoiceClient = class {
82
83
  this.pc.ontrack = (event) => {
83
84
  this.remoteStream = event.streams[0];
84
85
  this.emit("remoteStream", this.remoteStream);
85
- if (!this.isConnectedFlag) {
86
- this.isConnectedFlag = true;
87
- this.emit("connected");
88
- }
89
86
  };
90
87
  this.pc.onicecandidate = (event) => {
91
88
  if (event.candidate) {
@@ -101,8 +98,6 @@ var VoiceClient = class {
101
98
  this.isConnectedFlag = true;
102
99
  this.emit("connected");
103
100
  }
104
- } else if (this.pc?.connectionState === "disconnected") {
105
- this.emit("disconnected");
106
101
  }
107
102
  };
108
103
  }
@@ -124,11 +119,13 @@ var VoiceClient = class {
124
119
  switch (message.type) {
125
120
  case "user-joined":
126
121
  if (message.userId !== this.userId) {
122
+ this.targetUserId = message.userId;
127
123
  await this.createOffer();
128
124
  this.emit("userJoined", message.userId);
129
125
  }
130
126
  break;
131
127
  case "offer":
128
+ this.targetUserId = message.userId;
132
129
  await this.handleOffer(message);
133
130
  break;
134
131
  case "answer":
@@ -151,23 +148,36 @@ var VoiceClient = class {
151
148
  });
152
149
  }
153
150
  async handleOffer(message) {
154
- const offer = new RTCSessionDescription(message.sdp);
155
- await this.pc.setRemoteDescription(offer);
156
- const answer = await this.pc.createAnswer();
157
- await this.pc.setLocalDescription(answer);
158
- this.send({
159
- type: "answer",
160
- sdp: this.pc.localDescription
161
- });
151
+ try {
152
+ console.log("Handling offer", message);
153
+ const offer = new RTCSessionDescription(message.sdp);
154
+ await this.pc.setRemoteDescription(offer);
155
+ console.log("Remote description set");
156
+ const answer = await this.pc.createAnswer();
157
+ console.log("Answer created");
158
+ await this.pc.setLocalDescription(answer);
159
+ console.log("Local description set");
160
+ this.send({
161
+ type: "answer",
162
+ sdp: this.pc.localDescription
163
+ });
164
+ } catch (err) {
165
+ console.error("Offer handling error:", err);
166
+ }
162
167
  }
163
168
  async handleAnswer(message) {
164
169
  const answer = new RTCSessionDescription(message.sdp);
165
170
  await this.pc.setRemoteDescription(answer);
166
171
  }
167
172
  async handleCandidate(message) {
168
- if (message.candidate) {
169
- const candidate = new RTCIceCandidate(message.candidate);
170
- await this.pc.addIceCandidate(candidate);
173
+ try {
174
+ if (message.candidate) {
175
+ const candidate = new RTCIceCandidate(message.candidate);
176
+ await this.pc.addIceCandidate(candidate);
177
+ console.log("ICE candidate added");
178
+ }
179
+ } catch (err) {
180
+ console.error("Candidate error:", err);
171
181
  }
172
182
  }
173
183
  send(data) {
package/dist/index.mjs CHANGED
@@ -7,6 +7,7 @@ var VoiceClient = class {
7
7
  this.remoteStream = null;
8
8
  this.eventHandlers = /* @__PURE__ */ new Map();
9
9
  this.isConnectedFlag = false;
10
+ this.targetUserId = null;
10
11
  this.signalingUrl = config.signalingUrl;
11
12
  this.roomId = config.roomId;
12
13
  this.userId = config.userId;
@@ -56,10 +57,6 @@ var VoiceClient = class {
56
57
  this.pc.ontrack = (event) => {
57
58
  this.remoteStream = event.streams[0];
58
59
  this.emit("remoteStream", this.remoteStream);
59
- if (!this.isConnectedFlag) {
60
- this.isConnectedFlag = true;
61
- this.emit("connected");
62
- }
63
60
  };
64
61
  this.pc.onicecandidate = (event) => {
65
62
  if (event.candidate) {
@@ -75,8 +72,6 @@ var VoiceClient = class {
75
72
  this.isConnectedFlag = true;
76
73
  this.emit("connected");
77
74
  }
78
- } else if (this.pc?.connectionState === "disconnected") {
79
- this.emit("disconnected");
80
75
  }
81
76
  };
82
77
  }
@@ -98,11 +93,13 @@ var VoiceClient = class {
98
93
  switch (message.type) {
99
94
  case "user-joined":
100
95
  if (message.userId !== this.userId) {
96
+ this.targetUserId = message.userId;
101
97
  await this.createOffer();
102
98
  this.emit("userJoined", message.userId);
103
99
  }
104
100
  break;
105
101
  case "offer":
102
+ this.targetUserId = message.userId;
106
103
  await this.handleOffer(message);
107
104
  break;
108
105
  case "answer":
@@ -125,23 +122,36 @@ var VoiceClient = class {
125
122
  });
126
123
  }
127
124
  async handleOffer(message) {
128
- const offer = new RTCSessionDescription(message.sdp);
129
- await this.pc.setRemoteDescription(offer);
130
- const answer = await this.pc.createAnswer();
131
- await this.pc.setLocalDescription(answer);
132
- this.send({
133
- type: "answer",
134
- sdp: this.pc.localDescription
135
- });
125
+ try {
126
+ console.log("Handling offer", message);
127
+ const offer = new RTCSessionDescription(message.sdp);
128
+ await this.pc.setRemoteDescription(offer);
129
+ console.log("Remote description set");
130
+ const answer = await this.pc.createAnswer();
131
+ console.log("Answer created");
132
+ await this.pc.setLocalDescription(answer);
133
+ console.log("Local description set");
134
+ this.send({
135
+ type: "answer",
136
+ sdp: this.pc.localDescription
137
+ });
138
+ } catch (err) {
139
+ console.error("Offer handling error:", err);
140
+ }
136
141
  }
137
142
  async handleAnswer(message) {
138
143
  const answer = new RTCSessionDescription(message.sdp);
139
144
  await this.pc.setRemoteDescription(answer);
140
145
  }
141
146
  async handleCandidate(message) {
142
- if (message.candidate) {
143
- const candidate = new RTCIceCandidate(message.candidate);
144
- await this.pc.addIceCandidate(candidate);
147
+ try {
148
+ if (message.candidate) {
149
+ const candidate = new RTCIceCandidate(message.candidate);
150
+ await this.pc.addIceCandidate(candidate);
151
+ console.log("ICE candidate added");
152
+ }
153
+ } catch (err) {
154
+ console.error("Candidate error:", err);
145
155
  }
146
156
  }
147
157
  send(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicemaster/core",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "WebRTC voice communication core library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -17,6 +17,7 @@ export class VoiceClient {
17
17
  private userId: string;
18
18
  private iceServers: RTCIceServer[];
19
19
  private isConnectedFlag = false;
20
+ private targetUserId: string | null = null;
20
21
 
21
22
  constructor(config: VoiceClientConfig) {
22
23
  this.signalingUrl = config.signalingUrl;
@@ -38,6 +39,8 @@ export class VoiceClient {
38
39
  this.eventHandlers.get(event)!.push(callback);
39
40
  }
40
41
 
42
+
43
+
41
44
  private emit(event: string, ...args: any[]): void {
42
45
  const handlers = this.eventHandlers.get(event);
43
46
  if (handlers) {
@@ -76,10 +79,6 @@ export class VoiceClient {
76
79
  this.pc.ontrack = (event) => {
77
80
  this.remoteStream = event.streams[0];
78
81
  this.emit('remoteStream', this.remoteStream);
79
- if (!this.isConnectedFlag) {
80
- this.isConnectedFlag = true;
81
- this.emit('connected');
82
- }
83
82
  };
84
83
 
85
84
  this.pc.onicecandidate = (event) => {
@@ -97,8 +96,6 @@ export class VoiceClient {
97
96
  this.isConnectedFlag = true;
98
97
  this.emit('connected');
99
98
  }
100
- } else if (this.pc?.connectionState === 'disconnected') {
101
- this.emit('disconnected');
102
99
  }
103
100
  };
104
101
  }
@@ -125,11 +122,13 @@ export class VoiceClient {
125
122
  switch (message.type) {
126
123
  case 'user-joined':
127
124
  if (message.userId !== this.userId) {
125
+ this.targetUserId = message.userId;
128
126
  await this.createOffer();
129
127
  this.emit('userJoined', message.userId);
130
128
  }
131
129
  break;
132
130
  case 'offer':
131
+ this.targetUserId = message.userId;
133
132
  await this.handleOffer(message);
134
133
  break;
135
134
  case 'answer':
@@ -154,14 +153,25 @@ export class VoiceClient {
154
153
  }
155
154
 
156
155
  private async handleOffer(message: any): Promise<void> {
157
- const offer = new RTCSessionDescription(message.sdp);
158
- await this.pc!.setRemoteDescription(offer);
159
- const answer = await this.pc!.createAnswer();
160
- await this.pc!.setLocalDescription(answer);
161
- this.send({
162
- type: 'answer',
163
- sdp: this.pc!.localDescription
164
- });
156
+ try {
157
+ console.log('Handling offer', message);
158
+ const offer = new RTCSessionDescription(message.sdp);
159
+ await this.pc!.setRemoteDescription(offer);
160
+ console.log('Remote description set');
161
+
162
+ const answer = await this.pc!.createAnswer();
163
+ console.log('Answer created');
164
+
165
+ await this.pc!.setLocalDescription(answer);
166
+ console.log('Local description set');
167
+
168
+ this.send({
169
+ type: 'answer',
170
+ sdp: this.pc!.localDescription
171
+ });
172
+ } catch (err) {
173
+ console.error('Offer handling error:', err);
174
+ }
165
175
  }
166
176
 
167
177
  private async handleAnswer(message: any): Promise<void> {
@@ -170,9 +180,14 @@ export class VoiceClient {
170
180
  }
171
181
 
172
182
  private async handleCandidate(message: any): Promise<void> {
173
- if (message.candidate) {
174
- const candidate = new RTCIceCandidate(message.candidate);
175
- await this.pc!.addIceCandidate(candidate);
183
+ try {
184
+ if (message.candidate) {
185
+ const candidate = new RTCIceCandidate(message.candidate);
186
+ await this.pc!.addIceCandidate(candidate);
187
+ console.log('ICE candidate added');
188
+ }
189
+ } catch (err) {
190
+ console.error('Candidate error:', err);
176
191
  }
177
192
  }
178
193