I'm new to SignalR and I really don't have a clue. I need to connect to a stream and retrieve some data. I have gotten some help from the owner, but they don't know how to create it with JavaScript.
They gave me the following C# code:
private static HubConnection _hubConnection;
private static IHubProxy _hubProxy;
_hubConnection = new HubConnection("https://sesignalr.esmartapi.com/realtime");
_hubProxy = _hubConnection.CreateHubProxy("timeseriesHub");
_hubProxy.On<ValueResult>("broadCastValue", RecieveServerStreamMessageObject);
_hubConnection.Start().Wait();
_hubProxy.Invoke("Subscribe", assetId).Wait();
private static void RecieveServerStreamMessageObject(ValueResult message)
{
Console.WriteLine(message.ValueTime.ToShortDateString() + " " + message.ValueTime.ToLongTimeString() + " - Value: " + message.Value + " (id " + message.AssetId + ")");
}
public class ValueResult
{
public string AssetId { get; set; }
public int ValueCategory { get; set; }
public DateTime ValueTime { get; set; }
public Double Value { get; set; }
public int ValueUnit { get; set; }
}
So I tried the connection using the following JavaScript code:
var connection = $.hubConnection('https://sesignalr.esmartapi.com/realtime');
var proxy = connection.createHubProxy('timeseriesHub');
// receives broadcast messages from a hub function, called "broadcastMessage"
proxy.on('broadcastMessage', function(message) {
console.log(message);
});
// attempt connection, and handle errors
connection.start({ jsonp: true })
.done(function(){ console.log('Now connected, connection ID=' + connection.id); })
.fail(function(){ console.log('Could not connect'); });
And running this gave the following error:
403 (Forbidden: JSONP is disabled.)
I think I'm supposed to recreate the C# code above with JavaScript, but my C# knowledge is limited and I have no experience with this. I'm not sure how to do this, any help is appreciated.
I don't think you can really connect to a stream using javascript, its more of a notification framework. You need to notify the javascript client that something has changed, and update either through making another server call, or from data in the notification you send.
That C# code is horrible by the way, Its trying to create a synchronous stream out of an async notification framework. Signalr is not going to work well this way. IMO.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.