problem description
I use the stomp.js plug-in to operate websocket in vue. After connecting, I store the object of stomp in sessionstorage to prevent users from refreshing the browser and losing this object
the environmental background of the problems and what methods you have tried
I have taken two methods:
1, save the websocket object under window, it is OK to do this, the code logic, the process can work, but when the browser refreshes, the object will be lost.
2. Store the resulting websocket object, that is, the stomp object, in sessionstorage with JSON.stringify (). When you want to use it, JSON.parse () takes out the object, but this method reports an error. From the printing in the console, I can see that I have transferred the string stored in sessionstorage back to the object, but still reported an error. NewTag.send is not a function
related codes
/ / Please paste the code text below (do not replace the code with pictures)
store objects in sessionStorage
let url = "ws://xxx:9008/websocket",
let stompClient = Stomp.client(url);
stompClient.connect({"token": token,"courseId":courseId}, function (frame) {
window.STOMP_CLIENT = stompClient
let client = JSON.stringify(stompClient)
sessionStorage.STOMP_CLIENT = client
}
)
take out the object for use and report an error send is ont a function
let testTag = sessionStorage.STOMP_CLIENT;
let newTag = JSON.parse(testTag)
console.log(typeof newTag)
console.log(newTag)
newTag.send("/teamsking/course/sign/start",{"token": token},
JSON.stringify({
"bean":beanId,
"classroomId":classroomId,
"courseId":courseId,
"userId":userId
})
);
what result do you expect? What is the error message actually seen?
Why is there a mistake here? According to reason, it should not affect the use after it is taken out of sessionstorage and converted into an object.