podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
408 lines (336 loc) • 14.4 kB
HTML
<html>
<head>
<title>PodChat Load Tester</title>
<!-- Development Mode -->
<script src="../dist/podchat-browser-bundle.js"></script>
<script type="text/javascript" src="./config.js"></script>
<!-- Production Mode -->
<!--<script type="text/javascript" src="podchat.js"></script>-->
<!--<script type="text/javascript" src="podchat-browser.min.js"></script>-->
<style>
button {
margin: 10px 0;
}
</style>
<script type="text/javascript">
// Development
var PodChat = window.POD.Chat;
var env = 'main'; // main | sandbox | integration
var params = {
appId: new Date().getTime(),
/**
* Main Server
*/
// socketAddress: 'wss://msg.pod.ir/ws', // {**REQUIRED**} Socket Address
// ssoHost: 'https://accounts.pod.ir', // {**REQUIRED**} Socket Address
// platformHost: 'https://api.pod.ir/srv/core', // {**REQUIRED**} Platform Core Address
// fileServer: 'https://core.pod.ir', // {**REQUIRED**} File Server Address
// podSpaceFileServer: 'https://podspace.pod.ir', // {**REQUIRED**} File Server Address
// serverName: 'chat-server', // {**REQUIRED**} Server to to register on
socketAddress: CONFIG[env].socketAddress,
ssoHost: CONFIG[env].ssoHost,
platformHost: CONFIG[env].platformHost,
fileServer: CONFIG[env].fileServer,
podSpaceFileServer: CONFIG[env].podSpaceFileServer,
serverName: CONFIG[env].serverName,
callServerName: CONFIG[env].callServerName,
/**
* Sand Box
*/
// socketAddress: "wss://chat-sandbox.pod.ir/ws",
// ssoHost: "https://accounts.pod.ir",
// platformHost: "https://sandbox.pod.ir:8043/srv/basic-platform",
// fileServer: 'https://core.pod.ir',
// podSpaceFileServer: 'https://podspace.pod.ir',
// serverName: "chat-server",
/**
* Integration
*/
// socketAddress: "ws://172.16.110.235:8003/ws",
// ssoHost: "http://172.16.110.76",
// platformHost: "http://172.16.110.235:8003/srv/bptest-core",
// fileServer: 'https://core.pod.ir',
// podSpaceFileServer: 'http://172.16.110.61:8780/podspace',
// serverName: "chatlocal",
token: CONFIG.token,
grantDeviceIdFromSSO: false,
enableCache: false,
fullResponseObject: false,
mapApiKey: CONFIG.mapApiKey,
reconnectOnClose: true,
httpRequestTimeout: 30000,
httpUploadRequestTimeout: 0,
asyncRequestTimeout: 60 * 1000,
asyncLogging: {
onFunction: true,
consoleLogging: true,
onMessageReceive: false,
onMessageSend: false,
actualTiming: false
},
callOptions: {
// callSocketAddress: "wss://online-stream.pod.ir/gsthandler",
// callTurnIp: "188.75.65.144",
callSocketAddress: "wss://46.32.6.187/gsthandler",
callTurnIp: "46.32.6.188",
callDivId: "call-div",
callVideo: {
minWidth: 320,
minHeight: 180
},
callAudioTagClassName: "podcall-audio",
callVideoTagClassName: "podcall-video"
},
};
var chatAgent = new PodChat(params);
var count = 1,
messages = [],
longest = {
time: 0,
uniqueId: ""
},
globalCount = 1000,
globalDelay = 200,
globalThreadId = 267401;
var globalLooperTimeout,
globalSendMessagesFunction,
globalGetThreadsFunction;
chatAgent.on("chatReady", function () {
globalSendMessagesFunction = function () {
if (count <= globalCount) {
var currdate = new Date();
var uniqueId = chatAgent.generateUUID();
messages[uniqueId] = currdate.getTime();
console.log(`{ ${count} } [SEND] [${uniqueId}] :: { ${count} } at ${('0' + currdate.getHours()).slice(-2)}:${('0' + currdate.getMinutes()).slice(-2)}:${('0' + currdate.getSeconds()).slice(-2)}:${currdate.getMilliseconds()}`);
chatAgent.sendTextMessage({
uniqueId: uniqueId,
threadId: globalThreadId,
textMessage: `{ ${count} } [SEND] [${uniqueId}] <br/> { ${count} } at ${('0' + currdate.getHours()).slice(-2)}:${('0' + currdate.getMinutes()).slice(-2)}:${('0' + currdate.getSeconds()).slice(-2)}:${currdate.getMilliseconds()}`,
messageType: 'TEXT',
systemMetadata: {
time: currdate,
uniqueId: uniqueId,
type: 'Load Test'
}
}, {
onSent: function (result) {
var receiveTime = new Date();
var duration = new Date().getTime() - messages[result.uniqueId];
if (duration > longest.time) {
longest.time = duration;
longest.uniqueId = result.uniqueId;
console.log(longest);
}
console.log(`[RECV] [${result.uniqueId}] at ${('0' + receiveTime.getHours()).slice(-2)}:${('0' + receiveTime.getMinutes()).slice(-2)}:${('0' + receiveTime.getSeconds()).slice(-2)}:${receiveTime.getMilliseconds()}`);
console.log(`[TIME] [${result.uniqueId}] = ${duration}`);
},
onDeliver: function (result) {
},
onSeen: function (result) {
}
});
count++;
globalLooperTimeout = setTimeout(globalSendMessagesFunction, globalDelay);
}
};
globalGetThreadsFunction = function () {
if (count <= globalCount) {
var currdate = new Date();
var uniqueId = chatAgent.generateUUID();
messages[uniqueId] = currdate.getTime();
console.log(`[SEND] [${uniqueId}] :: { ${count} } at ${('0' + currdate.getHours()).slice(-2)}:${('0' + currdate.getMinutes()).slice(-2)}:${('0' + currdate.getSeconds()).slice(-2)}:${currdate.getMilliseconds()}`);
chatAgent.getThreads({
// new: true
}, function (result) {
var receiveTime = new Date();
var duration = new Date().getTime() - messages[result.uniqueId];
if (duration > longest.time) {
longest.time = duration;
longest.uniqueId = result.uniqueId;
console.log(longest);
}
console.log(`[RECV] [${result.uniqueId}] at ${('0' + receiveTime.getHours()).slice(-2)}:${('0' + receiveTime.getMinutes()).slice(-2)}:${('0' + receiveTime.getSeconds()).slice(-2)}:${receiveTime.getMilliseconds()}`);
console.log(`[TIME] [${result.uniqueId}] = ${duration}`);
});
count++;
globalLooperTimeout = setTimeout(globalGetThreadsFunction, globalDelay);
}
};
});
/**
* Listen to Error Messages
*/
chatAgent.on("error", function (error) {
console.log("Error ", error);
});
/**
* Listen to Chat State Changes
*/
chatAgent.on("chatState", function (chatState) {
// console.log(chatState);
});
/**
* Listen to File Upload Events
*/
chatAgent.on("fileUploadEvents", function (event) {
console.log(event);
});
chatAgent.on("contactEvents", function (event) {
if (event.type == 'CONTACTS_SYNCED') {
console.log('Core contacts have been successfully synced with chat server.');
}
});
/**
* Listen to Thread Events
*/
chatAgent.on("threadEvents", function (event) {
var type = event.type;
// console.log(event);
switch (type) {
case "THREAD_LAST_ACTIVITY_TIME":
break;
case "THREAD_NEW":
break;
case "THREAD_ADD_PARTICIPANTS":
break;
case "THREAD_REMOVE_PARTICIPANTS":
break;
case "THREAD_LEAVE_PARTICIPANT":
break;
case "THREAD_REMOVED_FROM":
break;
case "THREAD_RENAME":
break;
case "THREAD_MUTE":
break;
case "THREAD_UNMUTE":
break;
case "THREAD_INFO_UPDATED":
break;
case "THREAD_UNREAD_COUNT_UPDATED":
break;
default:
break;
}
});
/**
* Listen to Message Events
*/
var chatOnMessageEventsObject = chatAgent.on("messageEvents", function (event) {
var type = event.type,
message = event.result.message;
console.log(event);
switch (type) {
case "MESSAGE_NEW":
/**
* Sending Message Seen to Sender after 5 secs
*/
// setTimeout(function () {
// chatAgent.seen({
// messageId: message.id,
// ownerId: message.ownerId
// });
// }, 5000);
// console.log('# Message Received => ' + message.message);
break;
case "MESSAGE_EDIT":
break;
case "MESSAGE_DELIVERY":
break;
case "MESSAGE_SEEN":
break;
default:
break;
}
});
/**
* Listen to System Events
*/
chatAgent.on("systemEvents", function (event) {
var type = event.type;
console.log(event);
switch (type) {
case "IS_TYPING":
// console.log(event.result.user.user + " is typing in thread #" + event.result.thread);
break;
default:
break;
}
});
/**
* Listen to User Events
*/
chatAgent.on('userEvents', function (event) {
var type = event.type;
console.log(event);
switch (type) {
case 'CHAT_PROFILE_UPDATED':
console.log('Chat Profile Has Been Updated', event);
break;
default:
break;
}
});
</script>
</head>
<body style="max-width: 600px;">
<p>POD CHAT <span id="reconnect">Reconnect...</span> | <span id="clearCache">Clear User Cache</span> | <span
id="deleteCache">Delete Cache</span></p>
Token: <input type="text" id="tokenInput" autocomplete="on" width="500">
<button id="tokenInputSet">Set Token</button>
<br>
<h4 id="userName"></h4>
<br>
<button id="stopAll">Stop All</button>
<br>
<button id="sendMessageTest">Start Send Message Load Test</button>
<br>
<button id="getThreadsTest">Start Get Threads Load Test</button>
</body>
<script>
document.getElementById("reconnect")
.addEventListener("click", function () {
chatAgent.reconnect();
});
document.getElementById("clearCache")
.addEventListener("click", function () {
chatAgent.clearCacheDatabasesOfUser();
});
document.getElementById("deleteCache")
.addEventListener("click", function () {
chatAgent.deleteCacheDatabases({
storage: true,
queues: true
});
});
document.getElementById('tokenInputSet').addEventListener('click', function (e) {
console.log('New Token Has been set: ', document.getElementById('tokenInput').value);
chatAgent.setToken(document.getElementById('tokenInput').value);
chatAgent.getUserInfo(function(result){
document.getElementById('userName').innerText = result.result.user.name;
});
});
document.getElementById('stopAll').addEventListener('click', function () {
globalLooperTimeout && clearTimeout(globalLooperTimeout);
});
document.getElementById('sendMessageTest').addEventListener('click', function () {
count = 1;
messages = [];
longest = {
time: 0,
uniqueId: ""
};
globalSendMessagesFunction();
});
document.getElementById('getThreadsTest').addEventListener('click', function () {
count = 1;
messages = [];
longest = {
time: 0,
uniqueId: ""
};
globalGetThreadsFunction();
});
</script>
</html>