objective-c - iOS chatt应用程序使用Ruby TCPServer,可以接收但不能发送消息。

标签 objective-c ruby ios

我正在尝试为 iOS 创建一个简单的聊天应用程序。它适用于接收消息,但不适用于发送消息。我使用 Ruby 和 TCPServer 作为聊天服务器(来自:twgr),它看起来像这样:

require 'socket'                                   

server = TCPServer.new("localhost", 80)             
chatters = []

def welcome(chatter)                               
  chatter.puts "Welcome Chatter!"
end

def broadcast(message, chatters)                   
  chatters.each do |chatter|
    chatter.puts message
  end
end

while (chatter = server.accept)                         
  Thread.new(chatter) do |c|
    welcome(chatter)                        
    chatters << chatter                            
    begin                                            
      loop do
        line = c.readline                            
        broadcast("#{line}", chatters)       
      end
    rescue EOFError                                  
      c.close
      chatters.delete(c)                             
      broadcast("Chatter has left", chatters)
    end
  end
end 

服务器似乎工作正常,因为我可以在两个本地 telnet 连接之间发送消息。我认为问题出在我的 iOS 客户端上。我用于发送/接收消息的代码如下所示(截至:http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server):

- (void)messageReceived:(NSString *)message 
{
    [messages insertObject:message atIndex:0];

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
    withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];
}

/*
 Method for creating messages. 
 New messages are added above older messages.
 It gets called when the user hits the "Send" button.
 */
- (IBAction)sendMessage:(id)sender {
    NSString *response  = [NSString stringWithFormat:messageField.text];
    NSData *data = [[NSData alloc] initWithData:[response  
    dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
    messageField.text = @"";
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {    
    switch (streamEvent) {
        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {

                // This NSLog message dosn't get called when I want to send messages from 
                // my iOS client.
                NSLog(@"Hello");  
                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer 
                        length:len 
                        encoding:NSASCIIStringEncoding];                        
                        if (nil != output) {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }
            }
        break;        
        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop]  
            forMode:NSDefaultRunLoopMode];
        break;
    }
}

当我发送消息时,我可以在 iOS 客户端上接收消息,但当我尝试发送消息时没有任何反应。客户端当前如下所示:

enter image description here

最佳答案

这虽然是一个棘手的问题,但我认为我已经解决了它(试错法)。消息接收部分似乎可以工作,因为 Ruby 的 put 方法会自动向字符串添加换行符,而 NSString 则不然。这一更改使其发挥作用:

NSString *response = [NSString stringWithFormat: @"%@\n", messageField.text];

关于objective-c - iOS chatt应用程序使用Ruby TCPServer,可以接收但不能发送消息。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8341768/

相关文章:

iphone - 在 Objective-C XCode 中将常量链接到 pch 时出错

ruby-on-rails - 无法在 Windows PC 上安装 dm-sqlite-adapter Gem

html - IMGKit 将 HTML 转换为 HTML 特定部分的 PNG

ios - 检查下载的 NSData 哈希

ios - UISearchController & UISearchBar 子类

iphone - iPhone 中的 VoIP 实现

iphone - 可达性帮助 - WiFi 检测

ruby-on-rails - 将文件写入 S3 时的字符编码问题

ios - 如何在 NSURLSession 中禁用每个套接字的多个请求?

ios - 如何在纵向方向上关闭 UISplitController 的模态呈现