Próbuję zbudować klienta PhoneGap iOS dla podstawowego serwera chat SignalR, który mam uruchomiony (ASP.NET MVC 4). Wszystko działa wspaniale, gdy uzyskuje się do niego dostęp ze strony w przeglądarce, ale po prostu nie mogę uzyskać połączenia z aplikacji PhoneGap. Oto odnośne części mojego kodu:Jak połączyć się z koncentratorem SignalR z aplikacji PhoneGap na iOS?
Server global.asax
protected void Application_Start()
{
// Register the default hubs route: ~/signalr * This must be registered before any other routes
RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Server web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
</configuration>
piasta Server
public class ChatHub : Hub
{
public void Send(string name, string message)
{
Clients.All.broadcastMessage(name, message);
}
}
PhoneGap klienta
<body>
<div data-role="page">
<div data-role="header">
<h1>Life As A Pixel</h1>
</div><!-- /header -->
<div data-role="content">
<label for="username">Name:</label>
<input type="text" name="username" id="username" value="" />
<label for="message">Message:</label>
<input type="text" name="message" id="message" value="" />
<br>
<input type="button" value="Send" id="sendmessage" />
</div><!-- /content -->
<div data-role="footer" data-position="fixed">
<h4></h4>
</div><!-- /footer -->
</div><!-- /page -->
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.js"></script>
<script type="text/javascript" src="js/jquery.signalR-1.0.0-rc1.min.js"></script>
<script type="text/javascript" src="http://www.mysite.com/signalr/hubs"></script>
<script type="text/javascript">
app.initialize();
</script>
<script type="text/javascript">
$(function() {
// Declare a proxy to reference the hub
jQuery.support.cors = true;
$.connection.hub.url = 'http://www.mysite.com/signalr';
var chat = $.connection.chatHub;
alert(chat);
//alert(chat);
// Create a function that the hub can call to broadcast messages.
//chat.client.broadcastMessage = function (name, message) {
//$('#discussion').append('<li><strong>' + name
// + '</strong>: ' + message + '</li>');
//};
// Set initial focus to message input box.
//$('#message').focus();
// Start the connection.
$.connection.hub.start({ jsonp: true }).done(function() {
alert("connected");
$('#sendmessage').click(function() {
// Html encode display name and message.
var encodedName = $('<div />').text($('#username').val()).html();
var encodedMsg = $('<div />').text($('#message').val()).html();
// Call the Send method on the hub.
chat.send(encodedName, encodedMsg);
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
}).fail(function() {
alert("Failed to connect");
});
});
</script>
</body>
Przeszedłem przez mnóstwo stron, które mówią o bitach i elementach, ale nie mogę tego rozgryźć.
Dzięki z góry, Jason
http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#crossdomain. Uważnie przeczytaj uwagi. – davidfowl
Dzięki za artykuł dfowler. Naprawdę usunąłem linię cors, o czym wspomniałem w notatkach, jako jeden z wielu kroków debugowania, przez które przeszedłem, ale to nie miało znaczenia. Nie natknąłem się na ten konkretny artykuł i wygląda całkiem nieźle, więc przejrzę go i zobaczę, czy jest coś jeszcze, co mogłoby pomóc. – Jason
Czy kiedykolwiek miałeś szczęście? Chciałem przetestować to samo. – user441521