Commit 4f096f2c authored by Daniel Sucno's avatar Daniel Sucno

feat: add basic worker

-registration functionality

-call functionality (this needs some revision because there is no audio)
parent 1aefa8dd
This diff is collapsed.
let ws; // WebSocket connection
let sessionId; // Janus session ID
let handleId; // Janus plugin handle ID
const transactions = {}; // Object to track transactions
const apiKey = "5786EA93T6QP1KoAdh17Ac49sz0p70MG6I9F7FrPeVl32LFw3u"; // Replace with your API Key
const janusServer = "wss://arwebrtc06.hiperpbx.com:8192/janus"; // Replace with your Janus server URL
let keepAliveInterval; // Interval for keep-alive
// Generate a unique transaction ID
function generateTransactionId() {
return Math.random().toString(36).substring(2, 12);
}
// WebSocket event handlers
function connectWebSocket() {
ws = new WebSocket(janusServer, 'janus-protocol');
ws.onopen = () => {
postMessage({ type: "log", message: "WebSocket connection opened." });
createSession();
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
postMessage({ type: "log", message: `Message received: ${JSON.stringify(message)}` });
handleJanusMessage(message);
};
ws.onerror = (error) => {
postMessage({ type: "log", message: `WebSocket error: ${error}` });
};
ws.onclose = () => {
postMessage({ type: "log", message: "WebSocket connection closed." });
clearInterval(keepAliveInterval);
postMessage({ type: "playAudio" });
};
}
// Create a Janus session
function createSession() {
const transactionId = generateTransactionId();
transactions[transactionId] = { type: "createSession" };
const createSessionMessage = {
janus: "create",
transaction: transactionId,
apisecret: apiKey,
};
ws.send(JSON.stringify(createSessionMessage));
}
// Attach SIP plugin
function attachPlugin() {
const transactionId = generateTransactionId();
transactions[transactionId] = { type: "attachPlugin", plugin: "janus.plugin.sip" };
const attachPluginMessage = {
janus: "attach",
plugin: "janus.plugin.sip",
session_id: sessionId,
transaction: transactionId,
apisecret: apiKey,
};
ws.send(JSON.stringify(attachPluginMessage));
}
// Register SIP extension
function registerSIP(extension, password) {
const registerMessage = {
janus: "message",
body: {
request: "register",
username: extension,
secret: password,
},
handle_id: handleId,
session_id: sessionId,
transaction: generateTransactionId(),
apisecret: apiKey,
};
ws.send(JSON.stringify(registerMessage));
}
// Send Keep-Alive for Janus session
function sendKeepAlive() {
if (sessionId) {
const keepAliveMessage = {
janus: "keepalive",
session_id: sessionId,
transaction: generateTransactionId(),
apisecret: apiKey
};
ws.send(JSON.stringify(keepAliveMessage));
postMessage({ type: "log", message: "Keep-Alive sent." });
}
}
// Initiate a call
function initiateCall(destination, sdp) {
console.log(sdp)
const callMessage = {
janus: "message",
body: {
request: "call",
uri: destination
},
jsep:{
type: "offer",
sdp: sdp
},
handle_id: handleId,
session_id: sessionId,
transaction: generateTransactionId(),
apisecret: apiKey,
};
ws.send(JSON.stringify(callMessage));
postMessage({ type: "log", message: `Calling ${destination}...` });
}
// Hang up a call
function hangupCall() {
const hangupMessage = {
janus: "message",
body: {
request: "hangup"
},
handle_id: handleId,
session_id: sessionId,
transaction: generateTransactionId(),
apisecret: apiKey,
};
ws.send(JSON.stringify(hangupMessage));
postMessage({ type: "log", message: "Call terminated." });
}
// Handle incoming Janus messages
function handleJanusMessage(message) {
const { transaction, janus, data, error, jsep } = message;
if (janus === "success") {
if (transaction && transactions[transaction]) {
const transactionInfo = transactions[transaction];
if (transactionInfo.type === "createSession") {
sessionId = data.id;
postMessage({ type: "log", message: `Session created: ${sessionId}` });
attachPlugin();
keepAliveInterval = setInterval(sendKeepAlive, 30000);
} else if (transactionInfo.type === "attachPlugin" && transactionInfo.plugin === "janus.plugin.sip") {
handleId = data.id;
postMessage({ type: "log", message: `SIP plugin attached: ${handleId}` });
postMessage({ type: "registered" });
}
}
} else if (janus === "event" && data) {
if (data.sip) {
if (data.sip.result === "accepted") {
postMessage({ type: "log", message: "Call accepted by the remote party." });
} else if (data.sip.result === "hangup") {
postMessage({ type: "log", message: "Remote party hung up the call." });
}
}
} else if (janus === "error") {
postMessage({ type: "log", message: `Error: ${error?.reason}` });
if (error.code === 458) {
ws.close();
}
} else if(jsep){
postMessage({type: "accepted", jsep})
}
console.log('wea',message)
if (transaction) {
delete transactions[transaction];
}
}
// Handle messages from the main thread
onmessage = (e) => {
const { type, extension, password, destination, sdp } = e.data;
if (type === "connect") {
connectWebSocket();
} else if (type === "register") {
registerSIP(extension, password);
} else if (type === "call") {
initiateCall(destination, sdp);
} else if (type === "hangup") {
hangupCall();
}
};
File added
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment