-
Notifications
You must be signed in to change notification settings - Fork 0
/
sip-client.html
119 lines (105 loc) · 3.94 KB
/
sip-client.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!-- sip_client.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SIP Client</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-bottom: 20px;
}
.button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
margin: 0 10px;
}
.button:hover {
background-color: #0056b3;
}
.error-message {
color: red;
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>SIP Client</h1>
<button id="initializeButton" class="button">Initialize SIP Client</button> <!-- Button to initialize SIP client -->
<button id="callButton" class="button">Call</button> <!-- Button to initiate SIP call -->
<script src="./node_modules/sip.js/" onload="onSipJsLoad()" onerror="onSipJsError()"></script> <!-- Include SIP.js library with onload and onerror events -->
<script>
let sipJsLoaded = false; // Flag to track whether SIP.js library is loaded
// Function called when SIP.js library is successfully loaded
const onSipJsLoad = () => {
console.log('SIP.js library loaded successfully');
sipJsLoaded = true; // Set flag to true indicating library is loaded
};
// Function called when loading SIP.js library fails
const onSipJsError = () => {
console.error('Failed to load SIP.js library');
document.getElementById('errorMessage').innerText = 'Failed to load SIP.js library. Please check your internet connection and try again.'; // Display error message on the page
};
// Function to register to SIP server and initialize SIP client
const registerToServer = () => {
console.log('Initializing SIP client...');
// Check if SIP.js library is loaded before proceeding
if (!sipJsLoaded) {
console.error('SIP.js library is not loaded. Cannot initialize SIP client.');
document.getElementById('errorMessage').innerText = 'SIP.js library is not loaded. Cannot initialize SIP client.'; // Display error message on the page
return;
}
// Your code to initialize the SIP client goes here
// For example:
const server = {
uri: 'sip.example.com', // Replace with your SIP server URI
username: 'username',
password: 'password',
};
// Create SIP User Agent
const userAgent = new SIP.UA({
uri: `sip:${server.username}@${server.uri}`,
transportOptions: {
server: server.uri,
wsServers: `wss://${server.uri}:5061`, // Use wss:// for secure WebSocket
},
authorizationUser: server.username,
password: server.password,
});
// Start SIP User Agent
userAgent.start().then(() => {
console.log('SIP client registered successfully');
}).catch(error => {
console.error('Error registering SIP client:', error);
});
};
// Function to initiate SIP call
const initiateCall = (calleeURI) => {
console.log('Initiating call to', calleeURI);
// Your code to initiate SIP call goes here
};
// Add event listener to initialize SIP client button
document.getElementById('initializeButton').addEventListener('click', () => {
console.log('Button clicked. Initializing SIP client...');
registerToServer(); // Initialize SIP client
});
// Add event listener to call button
document.getElementById('callButton').addEventListener('click', () => {
console.log('Call button clicked. Initiating SIP call...');
initiateCall('sip:[email protected]'); // Replace with the SIP URI of the callee
});
</script>
<div id="errorMessage" class="error-message"></div> <!-- Container for displaying error message -->
</body>
</html>