2017-03-02 11 views
2

WebSocket/SignalRのレスポンスに「\ n」が表示されているのですが、その理由を理解できないようです。応答オブジェクトのデータ型をJSONと同じように解析してみましたが、 "\ n"を取り除くことはできません。Websocket/SignalRレスポンスで「 n」を取得していますか?

この「\ n」を削除して他のJSONオブジェクト/レスポンスと同様に処理するにはどうすればよいですか?参照用

コード:

-(void)SignalR{ 

    WebServices *services = [[WebServices alloc] init]; 

    SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"xxx"]; 

    SRHubProxy *proxy = [hubConnection createHubProxy:@"xxx"]; 

    [services callGetSRAlertGroupNames:^(NSMutableArray *alertGroupNameArray){ 
     NSLog(@"SR ALERT GROUP NAMES: %@", alertGroupNameArray); 

     [services callGetSRNotificationGroupNames:^(NSMutableArray *notificationGroupNameArray) { 
      NSLog(@"SR NOTIFICATION GROUP NAMES: %@", notificationGroupNameArray); 

      NSArray *combinedArray=[alertGroupNameArray arrayByAddingObjectsFromArray:notificationGroupNameArray]; 

      // Register for connection lifecycle events 
      [hubConnection setStarted:^{ 

       NSLog(@"Connection Started"); 

       for (NSString *groupName in combinedArray){ 
        [proxy invoke:@"Subscribe" withArgs:@[groupName] completionHandler:nil]; 
       } 

      }]; 
      [hubConnection setReceived:^(NSString *data) { 

       NSLog(@"CONNECTION RECIEVED - %@",data); 

      }]; 
      [hubConnection setConnectionSlow:^{ 
       NSLog(@"Connection Slow"); 
      }]; 
      [hubConnection setReconnecting:^{ 
       NSLog(@"Connection Reconnecting"); 
      }]; 
      [hubConnection setReconnected:^{ 
       NSLog(@"Connection Reconnected"); 
      }]; 
      [hubConnection setClosed:^{ 
       NSLog(@"Connection Closed"); 
      }]; 
      [hubConnection setError:^(NSError *error) { 
       NSLog(@"Connection Error %@",error); 
      }]; 

      [hubConnection start]; 

     }]; 
    }]; 
} 

アウトログイン応答サンプル:

CONNECTION RECIEVED - { 
    A =  (
     "{ 
\n \"NotificationType\": 1, 
\n \"TelemetryDetails\": { 
\n \"serialNumber\": \"xxx\", 
\n \"name\": \"sf-top\", 
\n \"statusId\": 2, 
\n \"buildVersion\": \"xxx\", 
\n \"securityModeId\": 2, 
\n \"IP\": \"xxx\", 
\n \"priority\": 1, 
\n \"bandwidthUpload\": 0.00, 
\n \"bandwidthDownload\": 0.00, 
\n \"bandwidthInternal\": null, 
\n \"totalBandwidthUpload\": 3107397.00, 
\n \"totalBandwidthDownload\": 8078656.00, 
\n \"totalBandwidthInternal\": null, 
\n \"usage\": \"8078656/3107397\", 
\n \"lastUpdateTime\": \"2017-03-02T16:27:57.1736937Z\", 
\n \"buildVersionUpdatingInProgress\": false, 
\n \"transportType\": 2, 
+1

Signal Rの通常の応答ではない私の経験から、バックエンドが情報を送信するように設定されている可能性があります。 – Kiley

答えて

1

シンプル。

を使用でき

data = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 

しかし、問題は、明らかに、あなたのWebサービスから来ているので、このトリックは「汚い」ですが、私は推測します。

EDIT:あなたは出現を交換する

このログは何ですか?

[hubConnection setReceived:^(NSString *data) { 

     NSLog(@"CONNECTION RECIEVED - %@",data); 
     NSString *newString = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
     NSLog(@"NEW STRING - %@", newString); 
    }]; 

EDIT 2:

[OK]をあなたが完了ハンドラの外にこの変数を追加しようとすることができます。

__block NSString *newString; 

それとも、この

[hubConnection setReceived:^(NSString *data) { 

      NSLog(@"CONNECTION RECIEVED - %@",data); 

      [self replaceOccurrences: data]; 

}]; 

- (void)replaceOccurrences:(NSString *)data { 
    NSString *newString = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    NSLog("New String = %@", newString); 
} 

を試すことができます。しかし、あなたが本当にあなたのバックエンドをチェックする必要があり、この反応は、適切な形式ではありません。

+0

残念ながら、それは辞書であることを示すこの行に壊れました。データオブジェクトをNSDictionaryにすると、その辞書を記録すると "\ n"が得られます。データの送信方法に問題がある可能性があります。 – arcade16

+0

そのステートメントは次のエラーを引き起こします: - [__ NSDictionaryI stringByReplacingOccurrencesOfString:withString:]:認識できないセレクターがインスタンス0x6180002665c0に送信されました – arcade16

関連する問題