I’m making a basic chat application with JSTL and Jakarta WebSockets but need to be able to pass a user’s group code to the websocket on open. I thought about adding the following meta tag to the HTML file
<meta name="group" content="${group}"/>
since “group” is stored as a session attribute. I then try to access this data in the following javascript file (where the websocket is created). Unfortunately I cannot do this since document doesn’t exist outside of functions. I added a comment to the line I am referring to.
var wsUrl;
if (window.location.protocol == 'http:') { wsUrl = 'ws://'; }
else { wsUrl = 'wss://'; }
var group = document.querySelector('meta[name=group]').content; //How can i access this info??
var socket = new WebSocket(wsUrl + window.location.host + "/ChoreSplitter/message?group=" + "123");
socket.onmessage = function(event) {
var mySpan = document.getElementById("chat");
mySpan.innerHTML += event.data;
};
socket.onerror = function(event) {
console.log("Chat Error ", event)
}
function sendMsg() {
var msg = document.getElementById("msg").value;
if (msg) {
socket.send(msg);
}
document.getElementById("msg").value = "";
}
Anyone have suggestions for a better way to pass this session data on creation of my web socket? Could I potentially send this data from a servlet to the ServerEndpoint?