SignalR web client (Javascript Client) without ASP.NET?

Jak
Jak
Member
858 Points
132 Posts

Hi,

I have created SignalR hub (enabled for cross domain) in a project.

Configuration code as:

using System;
using System.Threading.Tasks;
using Microsoft.Owin.Cors;
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;
[assembly: OwinStartup(typeof(Startup1))]
public class Startup1
{
public void Configuration(IAppBuilder app)
{
//app.MapSignalR();
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
}
}

 

And Hub Class as:

public class CurrencyHub : Hub
{
public static System.Timers.Timer _Timer;
public void SendCurrency()
{
if (_Timer == null) {
_Timer = new System.Timers.Timer(2000);
_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
_Timer.Enabled = true;
_Timer.Interval = 1000;
}
}
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
Clients.All.sendCurrency(GetCurrency(""));
}
}

 

Now I have another project as SignalR JavaScript client. I have JavaScript code as:

<script type="text/javascript" src="https://192.168.16.154:8138/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="https://192.168.16.154:8138/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script type="text/javascript" src="https://192.168.16.154:8138/signalr/hubs"></script>
<link href="https://192.168.16.154:8138/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://192.168.16.154:8138/Scripts/RealTimeCurrency.js"></script>
<script type="text/javascript">
$(function () {
var RealTimeCurrencyHub = $.connection.currencyHub;
var i = 0;
RealTimeCurrencyHub.client.sendCurrency = function (Currency) {
if (IsUpdateCurrency) {
UpdateCurrency(Currency)
} else {
AddCurrency(Currency);
IsUpdateCurrency = 1;
}
};
$.connection.hub.start(function() {
RealTimeCurrencyHub.server.sendCurrency();
});
});
</script>

I have following error in browser console as:

Views: 10497
Total Answered: 1
Total Marked As Answer: 1
Posted On: 09-Sep-2015 03:02

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi jak,

set hub url as:

<script type="text/javascript">
$(function () {
var RealTimeCurrencyHub = $.connection.currencyHub;
$.connection.hub.url = "https://192.168.16.154:8138/signalr";
var i = 0;
RealTimeCurrencyHub.client.sendCurrency = function (Currency) {
if (IsUpdateCurrency) {
UpdateCurrency(Currency)
} else {
AddCurrency(Currency);
IsUpdateCurrency = 1;
}
};
$.connection.hub.start(function() {
RealTimeCurrencyHub.server.sendCurrency();
});
});
</script>

 

Posted On: 09-Sep-2015 03:21
 Log In to Chat