Why is my Websocket connection not working in the Developer Console? - websocket

I'm trying to connect to a server using websockets. I've set up the port on the server, and am trying to connect through the Dev console in Chrome (also tried Firefox and got the same result).
I connect from the console using:
var websocket = new WebSocket('ws://localhost:5001');
This gives me the message "undefined". However, if I do
websocket.readyState
I get "1".
I then do
websocket.binaryType = 'arraybuffer';
which prints "arraybuffer".
If I then do something like
websocket.send("1+1");
it says undefined.
However, if I do all of this in an HTML file with JavaScript, it connects fine and I get the result "2", so it looks as though the Websocket itself is ok, and what I'm typing in is ok, but it's something to do with it being in the Dev Console that's the issue.
I don't know anything much about setting up Websockets.

I solved this by adding an onmessage function to the websocket:
websocket.onmessage = function (result) {
console.log(result.data);
}

Related

Failed to connect (500) to bot framework using Direct Line

We have a bot running in Azure (Web App Bot) that I'm trying to embed on a website. The bot is based of the Bot Builder V4 SDK Tamplate CoreBot v4.9.2. At first I used the iframe to embed the bod. This worked but didn't provide the features we need, so now im changing it to use DirectLine.
My code on the webpage looks like this:
<script crossorigin="anonymous"
src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<div id="webchat" role="main"></div>
<script>
(async function () {
const res = await fetch('https://[my bot name here].azurewebsites.net/.bot/v3/directline/tokens/generate',
{
method: 'POST',
headers: new Headers({
'Authorization': "Bearer [my token here]"
})
});
const { token } = await res.json();
window.WebChat.renderWebChat(
{
directLine: await window.WebChat.createDirectLineAppServiceExtension({
domain: 'https://[my bot name here].azurewebsites.net/.bot/v3/directline',
token
})
},
document.getElementById('webchat')
);
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
</script>
After some struggles I managed to fetch a token from https://[my bot name here].azurewebsites.net/.bot/v3/directline.
And I can see the chat window on my webpage, but is says connecting for a while then it changes to Taking longer than usual to connect, like this:
In the Chrome console there is an error saying Failed to connect Error: Connection response code 500. When I check Chrome's Network tab I can see that the token generated completed with status 200 and that the websocket connection is open, like this:
----------EDIT---------
I just noticed that when go to https://[my bot name here].azurewebsites.net/.bot using a webbrowser, the resulting json is
{"v":"1.0.0.0.55fa54091a[some key?]","k":true,"ib":false,"ob":false,"initialized":true}
ib and ob should be true but are false, maybe this is part of the problem.
----------EDIT 2---------
OK so now I'm starting to go crazy.
Ashish helped me and at some point the ib and ob were true. They were true for most of yesterday. At some point yesterday they turned false for a short while (no more than 2 hours). I checked if someone had triggered the release pipeline but no recent releases. After that ib and ob magically turned true again and connecting to the direct line worked again.
Now this morning ib and ob were false again. And again no recent releases. I don't know what is causing this.
Does anybody know what's going on here or how to fix this? How do I find what causes ib and ob to be false?
Any help is appreciated! Thanks in advance. If you need more information, just ask and I'll post it.
If the ib and ob values displayed by the *.bot endpoint are false this means the bot and the Direct Line app service extension are unable to connect to each other.
Make sure you verify below things:
Double check the code for using named pipes has been added to the
bot.
Confirm the bot is able to start up and run at all. Useful
tools are Test in WebChat, connecting an additional channel, remote
debugging, or logging.
Restart the entire Azure App Service the bot
is hosted within, to ensure a clean start up of all processes.
Please check troubleshooting guide, it seems updated today. (still old date reflected some how, not sure why)

How to receive private messages from the stream using Socket.IO in .Net Core 3.0

I am trying to receive private meesages from SocketIO stream. I have IP address but I am getting problem in connection. Also, if connection will happen then also I have to authorize the connection using token then only I can get the messages. I dont how to implement this as I am vey much New to this thing.
I have tried with the following code from the stack overflow but no luck yet.
Also, can anybody tell me which library is best to use:
SocketIO4Net.Client
SocketIOClientDotNet
I am using 2nd one.
var socket = IO.Socket("myip");
socket.On("xx", async (data) =>
{
var test = await Update(data.ToString());
});
Can anybody help me on this. I am .net server side developer.Thanks in advance!!

An ajax call to a url that opens socket.io connection - fails

I have a node.js server running. It accepts messages via socket.IO.
I need to send messages to the server from some device. This device supports Javascript, but does not support socket.IO commands (it's strange, but that's what I have).
I tried to work around it in the following way:
On my server I put a send.html file that sends a socket.IO message:
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io('http://localhost:3000/');
socket.emit('message', 'messages sent');
</script>
If I just call this file (localhost/send.html) then everything works fine.
Then I try to call send.html via ajax from the device:
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://localhost/send.html", true);
xmlhttp.send();
</script>
and.. nothing happens.
If I try to call any other .html file from the device, then it works. This makes me think that a socket.IO connection cannot be opened by a file that was called by ajax.
However I couldn't find this fact anywhere and anyway, I still need to find a way to send data from the device to socket.IO.
Does anyone know how to solve this problem, or can think of an alternative solution?

Socket.io not calling connect event in IE8

I have an application which uses socket.io to communicate with the server. In all browsers (i.e. Google Chrome, Firefox, etc.) except IE, the connect event is triggered. See definition below.
var socket = io.connect('xxx.xxx.xxx.xxx:1337');
socket.on('connect', function () {
//code here
});
For some reason, in IE8 this event doesn't get fired. Is there something different I need to do to make that happen?
Check if your JS console shows an "Access is Denied" exception. If so, have a look at:
NodeJS-socket.io getting "Access is Denied" Exception

Pusherapp/Websocket not connecting

I'm trying to use pusherapp here but chromium's console just gives me "WebSocket is closed before the connection is established." My JS is below, any ideas as to what might be the problem?
<script type='text/javascript'>
$(document).ready(function(){
var pusher_key = "<%= Pusher.key %>";
var pusher_channel = "thirsty-<%= Rails.env %>";
var pusher = new Pusher(pusher_key, pusher_channel);
pusher.bind('push_comment', function(content) {
comment_html = '<li><p>' + content + '</p></li>'
$(comment_html).prependTo('#comments');
});
});
</script>
The question is asking the reason for the following error:
WebSocket is closed before the connection is established.
This is actually a generic WebSocket error which is logged by the browser when an attempt is made to close the WebSocket connection (by calling webSocketInstance.close()) before the connection has even been established.
The error is triggered by the Pusher JavaScript library try to close a connection, but is caused by bad network or browser conditions (e.g. online/offline reporting). All all cases (that I'm aware of) the Pusher JavaScript library will retry connecting until those connections are resolved (e.g. the Internet connection is restored).
More information and an example in the following answer:
What does "WebSocket is closed before the connection is established" mean?
A couple of points on the code above:
the Pusher constructor has a first parameter which is the application key. The second parameter is a map of key value options. So, you shouldn't be passing in a channel name as the second parameter. More info on the Pusher constructor here
The pusher.bind call is actually deprecated (docs being updated now). For channel events you should bind directly on the channel using channel.bind and for connection events you should bind on the Pusher.connection object.
It could happen if the internet connection is too slow or disconnected.
Cross check the pusher Credentials.
It you have cluster in pusher channel app make sure you write it in you code.

Resources