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.
Related
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.
This is my first try with Bot Framework (Nodejs). I want to test delayed messages, for example, my bot must answer after a 5 seconds after receiving the message.
So I tried with this code:
var builder = require('botbuilder');
var connector = new builder.consoleconnector().listen();
var bot = new builder.universalbot(connector);
bot.dialog('/', function (session) {
if (!session.userData.TimeoutStarted) {
session.send("I'll answer in 5 seconds");
session.userData.TimeoutStarted = true;
setTimeout(function() {
session.send("Answer after 5 seconds");
session.userData.TimeoutStarted = false;
}, 5000);
} else {
session.send("Bot is busy");
}
});
But this doesn't work. Callback function inside setTimeout fires, but all operations with session doesn't work at all.
So, I find possible solution here: How to send message later in bot framework and rewrite my code:
var builder = require('botbuilder');
var connector = new builder.ConsoleConnector().listen();
var bot = new builder.UniversalBot(connector);
bot.dialog('/', function (session) {
if (session.userData.Timeout > 0 && Date.now() - session.userData.Timeout > 5000)
session.userData.Timeout = 0;
if (!session.userData.Timeout) {
session.send("I'll answer in 5 seconds");
var reply = session.message;
setTimeout(function() {
reply.text = "Answer after 5 seconds";
bot.send(reply);
}, 5000);
session.userData.Timeout = Date.now();
} else {
session.send("Bot is busy");
}
});
This code works, but looks terrible with so many checks. So I have a few questions:
Why first code example doesn't work? I guess problem in the session lifetime and then what is session lifetime?
How to set session.userData in this examples? So In first code example I want to set it inside callback function inside setTimeout but it doesn't work too.
What is the best way to create delayed answers?
I just investigated this issue. Looks like there's a bug in ConsoleConnector that makes it impossible to send two messages using the same session object (above a given interval between messages, due to internal batching). As the state is also persisted during send, your delayed state update will also not work. If you added a call to session.save to your callback in setTimeout, it would persist the new state (but would still not send the message).
I believe your first example should work with ChatConnector (haven't had the chance to try though). I'll create a pull request with the fix to ConsoleConnector.
I hope this answers all your questions.
UPDATE
See this issue and the related pull request for more details.
UPDATE2
It works for me with ChatConnector, using this code:
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: '',
appPassword: ''
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
bot.dialog('/', function (session) {
if (!session.userData.TimeoutStarted) {
session.send("I'll answer in 5 seconds");
session.userData.TimeoutStarted = true;
setTimeout(function() {
session.send("Answer after 5 seconds");
session.userData.TimeoutStarted = false;
}, 5000);
} else {
session.send("Bot is busy");
}
});
For people who are interested in sending a delayed message from bot, you could use session.delay(<ms>)
For example,
session.send('msg')
session.delay(5000) // delay 5 seconds
session.endDialog()
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.
Newbie to this - This code is works - in that the call to the script does what it is supposed to but returns the condition 500 and I can not see why. I am looking for any suggestions or changes that I should be making to make this work.
Thanks to all who respond.
function get_update_odometer(vehicle_key,odometer_value){
var url = "[%Catalyst.uri_for('/invoice/dispatch_util/get_update_odometer')%]";
new Ajax.Request(url, {
method: 'get',
parameters: {
key: vehicle_key,
ovalue: odometer_value
},
asynchronous:false,
onSuccess: successFunc,
onFailure: failureFunc
});
var return_v = $('rcontainer').innerHTML;
document.getElementById('odometer').value = return_v;
return true;
}
function successFunc(response){
if (200 == response.status){
var container = $('rcontainer');
var content = response.responseText;
container.update(content);
}
}
function failureFunc(response){
alert("Call has failed " + response.status );
}
Error code is coming from server side, and you provided the client part.
So have a look if your server script get_update_odometer is working, is callable by your web server and etc ...
I want to send the filepath of a file on my server to the client in order to play it using a media player. How can I retrieve that string on the client side in order to concatenate it in the src attribute of a <video element without using sockets?
Server snippet:
res.set('content-type', 'text/plain');
res.send('/files/download.mp4');
This is how you make a request to the server without any frameworks. "/path_to_page" is the route you set to the page that is supposed to process the request.
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path_to_page', true);
xhr.onload = function(e) {
if (this.status == 200) {
console.log(this.responseText); // output will be "/files/download.mp4"
}
};
xhr.send();
}
You might also want to send some params.
var formdata = new FormData();
formdata.append("param_name", "value");
So you might for instance want to send the filename or such.
You just need to change 2 lines from the first code snippet. One would be
xhr.open('POST', '/path_to_page', true); // set to post to send the params
xhr.send(formdata); // send the params
To get the params on the server, if you are using express, they are in req.body.param_name
Which framework are you using??
You can declare base path of your project directory in ajax and the followed by your file.
jQuery.ajax({
type: "GET",
url: "/files/download.mp4",
});
Since you are using express (on node), you could use socket.io:
Server:
var io = require('socket.io').listen(80),
fs = require('fs');
io.sockets.on('connection', function (socket) {
socket.on('download', function(req) {
fs.readFile(req.path, function (err, data) {
if (err) throw err;
socket.emit('video', { video: data });
});
});
});
Client:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
...
// request a download
socket.emit('download', { path: '/files/download.mp4' });
// receive a download
socket.on('video', function (data) {
// do sth with data.video;
});
...
</script>
Edit: didnt notice you didnt want to use sockets. Still it is a viable solution.