2017-05-08 26 views
0

我已經創建了一個SignalR服務器,一個Web客戶端和一個使用PhoneGap Build構建的移動應用程序。當我使用lite-server,PhoneGap CLI或PhoneGap桌面應用程序啓動PhoneGap應用程序時,SignalR可以在Web客戶端和PhoneGap應用程序之間完美地工作。這包括使用PhoneGap移動應用程序連接到PhoneGap Desktop應用程序。唯一一次失敗的時候是我用PhoneGap構建它並在設備上安裝APK。我沒有解釋這個錯誤。PhoneGap(Build)&SignalR,除了在模糊設備上實際安裝的設備上無處不在

我已經通過服務器的WEB.CONFIG處理了CORS。這是一個普通的ASP.NET應用程序,它只有一個OWIN啓動文件來配置SignalR和Hub類。

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE, TOKEN" /> 
     <add name="Access-Control-Allow-Credentials" value="false"/> 
     </customHeaders> 
    </httpProtocol> 
</system.webServer> 

令人沮喪的是,那就是當$ .connection.hub.start失敗產生的誤差爲

當「deviceready」事件觸發我稱之爲「錯誤的協商請求時出錯」 initiateSignalR()

var appConfig = { 
    'serverUrl': "http://wolf-signalrserver.azurewebsites.net" 
} 

var initiateSignalR = function(){ 
    if ($.signalR) { 

    } 

    var username = prompt("username"); 
    var success = ` 
       <div class ="serverMessage"> 
        <span>--- WELCOME, ${username} ---</span> 
       </div> 
      ` 
    $('#chatbox').append(success) 




     $.connection.hub.logging = true 
     $.connection.hub.qs = { 'username': username } 
     $.connection.hub.url = appConfig.serverUrl + "/signalr"; 
     var chatHub = $.connection.chatHub 

     chatHub.client.broadcastMessage = function (name, message) { 
      console.log("Data Incoming ---- Name: " + name + " Message: " + message); 
      var message = ` 
       <div class="row"> 
        <span class="col-sm-2">${name}:</span> 
        <span class="col-sm-9"> ${message}</span> 
       </div> 
      ` 
      $('#chatbox').append(message) 
     } 


     $('#messageButton').on('click', function() { 
      chatHub.server.send(username, $('#messageBox').val()) 
     }) 
     $('#messageForm').on('submit', function (e) { 
      e.preventDefault(); 
      chatHub.server.send(username, $('#messageBox').val()) 
     }) 

     $.connection.hub.start({ withCredentials: false, jsonp: true }) 
      .done(function() { 
       console.log("Connected successfully to SignalR"); 
       var success = ` 
        <div class ="serverMessage"> 
         <span>--- Successfully connected to the server ---</span> 
        </div> 
       ` 
       $('#chatbox').append(success) 
      }) 
      .fail(function (err) { 
       console.log("Unable to connect to SignalR") 
       alert(err); 
      }) 
} 

我有這樣的混帳回購協議在這裏:https://github.com/PhilWolf91/SignalR

我實現基於這裏有大量的問題,但他們沒有一個詳細的答案來解決這個問題。 例如:SignalR .Net Client fails with 500 server error on device, on simulator works fine

回答

0

好的。我是一個巨大的白癡。當我清理Config.Xml時,我刪除了網絡權限和默認的白名單插件。安裝在設備上爲這個項目

<?xml version='1.0' encoding='utf-8'?> 
<widget id="com.wolf.signalrphonegap" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0"> 
    <name>SignalRPhoneGap</name> 
    <description> 
     Hello World sample application that responds to the deviceready event. 
    </description> 
    <author email="[email protected]" href="http://phillipwolf91.wordpress.com"> 
     Wolf Software 
    </author> 

    <feature name="http://api.phonegap.com/1.0/network" /> 
    <gap:plugin name="cordova-plugin-whitelist" source="npm" /> 
    <content src="index.html" /> 
    <preference name="DisallowOverscroll" value="true" /> 
    <preference name="android-minSdkVersion" value="14" /> 
    <platform name="android"> 
    </platform>  
    <access origin="*" /> 
    <allow-intent href="http://*/*" /> 
    <allow-intent href="https://*/*" /> 
    <allow-intent href="tel:*" /> 
    <allow-intent href="sms:*" /> 
    <allow-intent href="mailto:*" /> 
    <allow-intent href="geo:*" /> 
    <platform name="android"> 
     <allow-intent href="market:*" /> 
    </platform> 

</widget> 

我現在已經SignalR服務器之間的工作,Web客戶端和應用程序的PhoneGap

<feature name="http://api.phonegap.com/1.0/network" /> 
<gap:plugin name="cordova-plugin-whitelist" source="npm" /> 

全功能配置