I have 2 hubs that live on 2 different servers. Ideally I would like my series of webpages to connect to attempt to connect to 1 of these hubs, then if it fails switch to the other hub. Is this possible at all.
I can't use the traditional method to connecting to the hub like so:
<script id="hubScript" src="IP:port/signalr/hubs"></script>
$.connection.hub.url = SIGNALR_HUB_SERVER_1;
srv = $.connection.tcpHub;
since I don't know which hub I will be connecting to, or which hub will be alive.
Example of code that I have tried for reference
if (document.head.getElementsByTagName("script").item(i).src == SIGNALR_HUB_SERVER_1 + "/hubs") {
$.connection.hub.url = SIGNALR_HUB_SERVER_1;
}
else if(document.head.getElementsByTagName("script").item(i).src == SIGNALR_HUB_SERVER_2 + "/hubs") {
$.connection.hub.url = SIGNALR_HUB_SERVER_2;
}
When I try and connect using this code I get the error: "Error loading hubs. Ensure your hubs reference is correct". This is because my script tag that contains the url is not the same as the url that I am trying to connect to.
Related
I have an ASP.NET Core server, which is using SignalR to generate HTML pages dynamically at runtime through JavaScript. After i shut my application down, in the console i can read that SignalR is disconnected:
signalr.js:2404 Error: Connection disconnected with error 'Error: Websocket closed with status code: 1006 ()'.
The question is - what do i need to do to setup SignalR to wait incoming connection again after disconnecting? Meaning that when my application will be up again, preloaded localhost page will connect automatically and continue working with the new instance of my app.
The way i initialize SignalR in JS:
var connection = new signalR.HubConnectionBuilder()
.withUrl("/foo")
.build();
And then
connection.on("state", function (state) {
// some business logics there
});
connection.start().catch(function err() {
return console.error(err.ToString());
});
As far as I know, Signalr Client library provide the auto reconnect feature. You could enable it by using the withAutomaticReconnect. Please notice: Without any parameters, WithAutomaticReconnect configures the client to wait 0, 2, 10, and 30 seconds respectively before trying each reconnect attempt. After four failed attempts, it stops trying to reconnect.
Also you could write codes to do Manually reconnect. More details, you could refer to this article.
Brando's answer lead me to do the following thing:
var connection = new signalR.HubConnectionBuilder()
.withUrl("/foo")
.build();
function start() {
connection.start().catch(function () {
setTimeout(function () {
start();
//there we can process any HTML changes like
//disabling our loader etc. start() will fail
//with exception if there's no server started
//and it will try again in 5 seconds
}, 5000);
});
}
connection.onclose(e => {
//process here any HTML changes like
//showing the loader, etc, an then
start();
})
connection.on("foo", function (state) {
// some business logics there
});
start();
It looks like typical relationship between ASP.NET and signalR + vanilla javascript
I am writing a chat app with Spring Boot web sockets and Stomp on the front end. Is there a way to programmatically access context root of my application in JavaScript?
my application.properties:
server.contextPath=/chatapp
my js
function connect() {
socket = new SockJS('/chatapp/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
stompClient.subscribe('/user/queue/messages', function (message) {
showMessage(JSON.parse(message.body));
});
}
}
Now if I want to change application context in properties I will have to remember to change it in several places in the project which I am trying to avoid. So instead of hardcoding 'chatapp' in js I want to be able to extract it programmatically.
I know in jsp one can access context path through ${pageContext.request.contextPath} but I am using thymeleaf.
I had built interception calls in the socket.io file located under node_modules/socket.io/lib/client.js with version 1.3.7 (at least I think so, however I have to update to 1.4.5 because of other requirements). These changes allowed spoof information coming from the sender socket and they were done before continuing to send the data to the receiver socket.
Before (around 1.3.7), the method ran before sending a packet was the following:
Client.prototype.packet = function(packet, preEncoded, volatile){
var self = this;
var sockets = this.sockets[0]; //this helds the socket object
but now (1.4.5) socket.io changed its call to the following
Client.prototype.packet = function(packet, opts){
var sockets = this.sockets[0]; //gives undefined
I tried to look throughout the given objects but couldn't find the sockets of the receiver user.
Back in 1.3.7 I was able to effortlessly give properties to a socket object (e.g: socket.some-property = 1; in the .js file ran by nodejs in the root of the server) and later be able to get this some-property back in node_modules/client.js whenever the receiver got some packet so I could intercept the call but now it does not work and I would like to apply my old code to this new context in order for it all to function again.
var socketObject = {};
io.sockets.on('connection', function (client) {
socketObject[client.id] = {socket: client};
client.on('data', function (somedata) {
socketObject[client.id].data = someData;
});
client.on('disconnect', function() {
delete socketObject[client.id];
});
});
I am trying to stream data from a background server application to a client-side web-page using gevent-websocket.
When using localhost or leaving ('',8999) I am able to connect to the web-socket, but when trying to access from off the server I cannot seem to connect with the error 'Firefox can't establish a connection to the server at ws://SERVER-IP:8999/.'
I have tried other browsers with the same issue (well I tried chrome as well)
def app(environ, start_response):
ws = environ['wsgi.websocket']
stream_listener.add_socket(ws)
while not ws.closed:
gevent.sleep(0.1)
server = pywsgi.WSGIServer(
('0.0.0.0', 8999), app, handler_class=WebSocketHandler)
server.serve_forever()
As I said I have tried leaving it blank - putting in '0.0.0.0' in hopes that would bind it to all interfaces, but still no luck.
Here is the client side script that works from localhost - but trying to put in the SERVER-IP:8999 fails.
var ws = new WebSocket("ws://SERVER-IP:8999/");
ws.onopen = function() {
ws.send("Hello, world")
}, ws.onmessage = function(a) {
var b = JSON.parse(a.data);
//do something with b
};
I am working on a node.js application that will connect to a UNIX socket (on a Linux machine) and facilitate communication between a web page and that socket. So far, I have been able to create socket and communicate back and forth with this code in my main app.js:
var net = require('net');
var fs = require('fs');
var socketPath = '/tmp/mysocket';
fs.stat(socketPath, function(err) {
if (!err) fs.unlinkSync(socketPath);
var unixServer = net.createServer(function(localSerialConnection) {
localSerialConnection.on('data', function(data) {
// data is a buffer from the socket
});
// write to socket with localSerialConnection.write()
});
unixServer.listen(socketPath);
});
This code causes node.js to create a UNIX socket at /tmp/mysocket and I am getting good communication by testing with nc -U /tmp/mysocket on the command line. However...
I want to establish a connection to an already existing UNIX socket from my node.js application. With my current code, if I create a socket from the command line (nc -Ul /tmp/mysocket), then run my node.js application, there is no communication between the socket and my application (The 'connect' event is not fired from node.js server object).
Any tips on how to go about accomplishing this? My experiments with node.js function net.createSocket instead of net.createServer have so far failed and I'm not sure if that's even the right track.
The method you're looking for is net.createConnection(path):
var client = net.createConnection("/tmp/mysocket");
client.on("connect", function() {
... do something when you connect ...
});
client.on("data", function(data) {
... do stuff with the data ...
});
I was just trying to get this to work with Linux's abstract sockets and found them to be incompatible with node's net library. Instead, the following code can be used with the abstract-socket library:
const abstract_socket = require('abstract-socket');
let client = abstract_socket.connect('\0my_abstract_socket');
client.on("connect", function() {
... do something when you connect ...
});
client.on("data", function(data) {
... do stuff with the data ...
});
You can also connect to a socket like this:
http://unix:/path/to/my.sock: