Where did you get this from? WebSocket
constructor only has two arguments, so your code makes absolutely no sense.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket
can u tell me how to pass the cookie then?
thank you
Your browser passes it automatically.
Still curious where you found that code. I'm sure you didn't just write random nonsense. There must be some resource where you found it.
so it should work like this:
const establishHttpSession = async () => {
await axios.get('https://demo.traccar.org/api/session?token=<my-token>);
const socket = new WebSocket("wss://demo.traccar.org/api/socket");
socket.onopen = () => {
console.log('WebSocket connection established');
};
socket.onerror = (error) => {
console.log('WebSocket connection failed:', error);
console.log('Ready state:', socket.readyState);
console.log('Error event:', error.event);
};
i found it on stackoverflow , i thought it was correct,mb ,i will try find the post
it still doesnt work with the new code
I hope you're not actually using "demo.traccar.org". Are you familiar with CORS restrictions?
i cant use the demo to connect to the websocket?
and no i'm not familiar with CORS restrictions, im still beginner ,im using traccar for a college project
hey anton,so the CORS is not enable in the demo ,so i tried to set up a proxy:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api/socket',
createProxyMiddleware({
target: 'https://demo4.traccar.org',
changeOrigin: true,
ws: true,
})
);
};
and this the connection code:
const establishHttpSession = async () => {
await axios.get(
'https://demo4.traccar.org/api/session?token=<mytoken>
);
};
useEffect(() => {
const establishConnection = async () => {
await establishHttpSession();
const protocol = 'wss:'
const host = window.location.host
const wsUrl =`${protocol}//${host}/api/socket`;
const socket = new WebSocket(wsUrl);
socket.onopen = () => {
console.log('WebSocket connection established');
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
socket.onmessage = (message) => {
console.log('WebSocket message:', message);
};
socket.onclose = (event) => {
console.log('WebSocket connection closed:', event);
};
}
establishConnection();
}, [])
the console just return WebSocket connection closed
I think you still don't understand CORS. You're creating a session with different host.
hi there , im trying to use to connect the websocket to the server but no luck,i did get the session
cookie:JSESSIONID=node01adwd8tgcec1x4r7bkfh7sl64584.node0
and then pass it in the websocket:const establishWebSocketConnection = (cookie) => { const socket = new WebSocket("wss://demo.traccar.org/api/socket", [], { 'Cookie': cookie }); socket.onopen = () => { console.log('WebSocket connection established'); }; socket.onerror = (error) => { console.log('WebSocket connection failed:', error); console.log('Ready state:', socket.readyState); console.log('Error event:', error.event); }; setSocket(socket); };
can someone help me