我正在尝试使用 Sencha Touch 2.1 和 PhoneGap 2.5.0 从本机 Android 应用程序中连接到 SignalR。
这是我到目前为止所拥有的:
我的 SignalR 服务器是在 Windows Server 2012 上运行的 ASP.NET MVC3 应用程序。跨域访问已在 global.asax.vb 中启用。
全球.asax.vb
Sub Application_Start()
Dim config As New HubConfiguration
config.EnableCrossDomain = True 'Allow cross domain (for native apps)
RouteTable.Routes.MapHubs(config)
AreaRegistration.RegisterAllAreas()
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
End Sub
我正在使用 SignalR 1.0.1 集线器。我已经实现了我的 Sencha Touch 应用程序调用的“身份验证”方法:
发射器.vb
Public Class Transmitter
Inherits Hub
Public Sub authenticate(ByVal sClientId As String, _
ByVal sPassword As String)
Try
Dim bRegistered As Boolean = ValidateUser(sClientId, sPassword)
If bRegistered Then
Clients.Caller.isAuthenticated(True)
Else
Clients.Caller.isAuthenticated(False)
End If
Catch ex As Exception
LogEvent("Authenticate: " & ex.Message)
End Try
End Sub
End Class
我开发了一个 Sencha Touch 2.1 应用程序,它连接到我的 SignalR 服务器并调用“身份验证”功能。这在 chrome 网络浏览器中运行良好。我在 index.html 文件中添加了对 SignalR 的引用,如下所示:
索引.html
<script src="https://www.mySite.com/SignalRServer/Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="https://www.mySite.com/SignalRServer/Scripts/jquery.signalR-1.0.1.js" type="text/javascript"></script>
<script src="https://www.mySite.com/SignalRServer/signalr/hubs"></script>
这是用于连接到 SignalR 并从我的 Sencha Touch 应用程序中调用“authenticate”方法的代码:
MySenchaTabPanel.js
listeners: {
activate: function () {
$.connection.hub.logging = true;
$.connection.hub.url = 'https://www.mySite.com/SignalRServer/signalr';
var transmitterProxy = $.connection.transmitter;
transmitterProxy.client.isAuthenticated = function (bAuthenticated) {
console.log('isAuthenticated: ' + bAuthenticated);
};
console.log('Connecting to server...');
$.connection.hub.start()
.done(function () {
console.log('Connected to Server: Transport: ' + $.connection.hub.transport.name);
transmitterProxy.server.authenticate(sClientId, sPassword);
})
.fail(function (error) {
console.log('Could not connect to server:' + error);
});
}
}
我现在使用PhoneGap 2.5.0 来包装Sencha Touch 2.1 应用程序,并将它部署在Android 4.2.2 模拟器上。应用程序在启动连接时显示在完成回调函数中找到的“已连接到服务器”日志。但是当它试图调用'authenticate'函数时什么也没有发生。这是日志:
03-26 05:29:38.631: D/CordovaLog(1758): exception firing pause event from native
03-26 05:29:49.978: D/CordovaLog(1758): Connecting to server...
03-26 05:29:51.330: D/CordovaLog(1758): exception firing destroy event from native
03-26 05:29:51.688: D/CordovaLog(1758): [05:29:51 GMT+0000 (GMT)] SignalR: Auto detected cross domain url.
03-26 05:29:51.688: D/CordovaLog(1758): [05:29:51 GMT+0000 (GMT)] SignalR: Negotiating with 'https://www.mySite.com/SignalRServer/signalr/negotiate'.
03-26 05:29:52.840: D/CordovaLog(1758): [05:29:52 GMT+0000 (GMT)] SignalR: SignalR: Initializing long polling connection with server.
03-26 05:29:53.988: D/CordovaLog(1758): [05:29:53 GMT+0000 (GMT)] SignalR: Attempting to connect to 'https://www.mySite.com/SignalRServer/signalr/connect?transport=longPolling&connectionToken=fW1SyP5V9Qwq7d0WF3FBn8Fr9rXvNJZ-VryuonW7LlsUyIbUlQ5LlW9gTRJNGch9t8yO4te8fbyZW2ZPKY_eAdnvIz6tHKxX0rmq8qNa8tv0budwIBjwD6YM1j4pSDTs0&connectionData=%5B%7B%22name%22%3A%22transmitter%22%7D%5D&tid=2' using longPolling.
03-26 05:29:54.258: D/CordovaLog(1758): Connected to Server: Transport: longPolling
03-26 05:29:54.358: D/CordovaLog(1758): [05:29:54 GMT+0000 (GMT)] SignalR: Longpolling connected
03-26 05:29:54.799: D/CordovaLog(1758): [05:29:54 GMT+0000 (GMT)] SignalR: Triggering client hub event 'joined' on hub 'Transmitter'.
03-26 05:29:54.808: D/CordovaLog(1758): [05:29:54 GMT+0000 (GMT)] SignalR: Attempting to connect to 'https://www.mySite.com/SignalRServer/signalr?transport=longPolling&connectionToken=fW1SyP5V9Qwq7d0WF3FBn8Fr9rXvNJZ-VryuonW7LlsUyIbUlQ5LlW9gTRJNGch9t8yO4te8fbyZW2ZPKY_eAdnvIz6tHKxX0rmq8qNa8tv0budwIBjwD6YM1j4pSDTs0&connectionData=%5B%7B%22name%22%3A%22transmitter%22%7D%5D&messageId=B%2C45%7CBo%2C0%7CBp%2C0%7CBq%2C0&tid=10' using longPolling.
当我在三星 Galaxy Android 4.1.2 设备上部署应用程序时看到相同的结果(在我指定传输协议以解决 Android 和 SignalR 的已知问题之后)。有人可以帮我让 SignalR 在 Android 上运行吗?