Is there any way to do this?
Client side:
function connectWebSocket() {
var socket = new SockJS('/socket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected");
});
}
Server side is not important. After the code above has been executed I need to know my session id.
You can get it from url without making any changes into SockJS library.
var socket = new SockJS('/mqClient');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log(socket._transport.url);
//it contains ws://localhost:8080/mqClient/039/byxby3jv/websocket
//sessionId is byxby3jv
});
The SockJS constructor has an option parameter and there you can pass a custom session id generator as a function:
let sessionId = utils.random_string(8);
let socket = new SockJS('/socket', [], {
sessionId: () => {
return sessionId
}
});
To get session id we need to make some changes into SockJS library.
The string
var connid = utils.random_string(8);
is used to get our id. So, we need only complete it like this:
var connid = utils.random_string(8);
that.sessionId = connid;
and then we can read this field from the client code:
function connectWebSocket() {
var socket = new SockJS('/socket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected, session id: " + socket.sessionId);
});
}
And if we need to know session id before calling connect method we can modify SockJS' constructor and connect method to use client-passed value.
Related
is it save to just add a property with a numerical id to a new websocket object and to identify a client with this id which was added by the server ?
server.js
//...
var a_o_websocket_client = []
o_websocket_server.on("connection", async function (o_websocket_client) {
console.log(`a new o_websocket_client connected: ${o_websocket_client}`)
o_websocket_client.n_id = a_o_websocket_client.length; // assign a new id
a_o_websocket_client.push(o_websocket_client)
o_websocket_client.on(
"message",
async function(s_message){
console.log(`this is the websocket with the id ${n_id}`)
}
)
});
//...
Here is my code:
var socket = require('socket.io-client')('http://127.0.0.1:3000/printers', {
query: "shop=" + "123456",
transports: ["websocket"]
});
If I delete query, I can connect to socket. Where am I wrong?
There doesn't seem to be anything wrong with your client-side code. I can connect by copying and pasting your code.
I suspect the problem is within your server-side code. Here is an example I am using with your client-side code:
var http = require('http');
var io = require('socket.io');
var server = http.createServer(function(req,res){
res.writeHead(200);
res.end('Hello, World!\n');
});
server.listen(80);
var socket = io.listen(server);
socket.use(function(socket, next){
console.log("Query: ", socket.handshake.query);
if (socket.handshake.query.shop == '123456') {
next();
}
next(new Error('You cannot use that shop'));
});
socket.on('connection', function(client) {
console.log('New Connection');
});
I'm able to obtain the query data in the 'socket.use' function. If I don't call the next() function, the client will never get the message that the server has received the response and is connected.
I recommend checking out the example used in this thread.
My Connection to websocket as below :
var sockJSEndpoint = "/myproject/mywebsocket";
var socket = new SockJS(sockJSEndpoint);
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected : ' + frame);
stompClient.subscribe('/topic/id/', function (data) {
requestId = getRequestId(data.body);
stompClient.subscribe('/topic/progress/' + id, function(data) {
progressHandler(data.body,id);
});
});
});
This code works fine on the mac but when deployed to Linux the /topic/id/ call does not return and response and hence the second call does not get triggered. And this happens only for the first request .Subsequent requests go through fine , as soon as the first call get triggered it returns the Id and attaches to next call and then the progress can be tracked. Any idea why it fails the first time ?
Any help is appreciated.
I want to use websocket with SockJs and Stomp client-side, and Java server-side, in a Spring-boot web application. I have installed with npm sockjs-client and stompjs. My client-side code is:
var SockJS = require('sockjs-client');
var Stomp = require("stompjs");
var socket = new SockJS('/webSocketEndPoint');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
stompClient.subscribe('/topic/notification', function (notificationBody) {
alert(JSON.parse(notificationBody.body).content);
});
})
The methods over and subscribe are not working, so the websocket i want to build is not working. IntelliJ say me "Unresolved function or method over" and "Unresolved function or method subscribe ". I've included in my package.json stompjs and sockjs-client (correct version). The initial requires not makes me problem, but i'm not able to run this script..Someone can help me?! Thanks a lot
Maybe because of wrong quotes in require("stompjs")?
Here is my workin example:
let SockJS = require('sockjs-client');
const Stomp = require('stompjs');
function register(registrations) {
let socket = SockJS('/webSocketEndPoint');
let stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
registrations.forEach(function (registration) {
stompClient.subscribe('/topic/notification', function (notificationBody) {
alert(JSON.parse(notificationBody.body).content);
});
});
});
}
I can't seem to call a server function from my client using either socket.io or an ajax call. Any help would be appreciated. For socket.io, I was trying something like this:
server (no error is being thrown, only I never see the console.log):
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var socket = io.sockets.on('connection', function(socket) {});
socket.on('admin-refresh', function() {
console.log("*** Admin refresh ***");
});
client (yes I'm including all necessary files, no error is being thrown on client side nor server):
$('document').ready(function() {
var socket = io();
$('#refresh').click(function() {
console.log('refresh clicked..');
io.emit('admin-refresh');
});
});
I don't need any data to be passed, I just want to alert the server to call a function. So perhaps an ajax call would be easier? How would I set up the server to listen for calls?
Why do you have an empty connection callback..? Look at the docs again.
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.on('admin-refresh', function() {
console.log("*** Admin refresh ***");
});
});