Objective-C网络 - 最佳实践? [英] Objective-C networking - best practices?

查看:202
本文介绍了Objective-C网络 - 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有服务器和客户端的Objective-C应用程序。客户端可以向服务器发送更新,并且服务器需要能够向每个连接的客户端发送更新。我一直在考虑如何最好地实现这个系统,但是要求您的建议。

I'm building an Objective-C app that has both a server and a client. The client can send updates to the server, and the server needs to be able to send updates to each connected client. I've been thinking about how best to implement this system, but am asking for your suggestions.

目前,我认为当有新的更新可用,服务器将使用线程依次向每个客户端发送更新。如果客户端超时,它们将断开连接。
我有很少的网络体验,所以请问你的见解。

Currently, I'm thinking that when new updates are available, the server will use threads to send the update to each client in turn. If a client times out, they are disconnected. I have very little networking experience, so am asking your insight.

你认为这个系统会工作得很好吗?
如果是这样,你有什么建议如何做线程?你可以指点我的任何NS课程?

Do you think that this system would work well? If so, do you have any suggestions about how to do the threading? Any NS classes you can point me at? There's got to be some kind of queue I can use, I'm thinking.

还有其他想法吗?

编辑:我不希望客户端数量超过50左右,最大。

I do not expect the client count to get much above 50 or so, at the max.

推荐答案

客户端和服务器都是OS X应用程序,并且都可以使用Cocoa框架在Objective-C中编写,我强烈建议您查看分布式对象(DO)技术。我不会尝试在分布式对象在这里给一个教程,只是解释为什么它可能是有用的...

As long as both client and server are OS X apps and can both be written in Objective-C using the Cocoa frameworks, I would highly recommend you take a look at the Distributed Objects (DO) technology in Cocoa. I won't try to give a tutorial in Distributed Objects here, just explain why it might be useful...

DO处理异步网络详细信息为您(所有客户端更新可能发生在单个线程)。此外,与远程对象(客户端到服务器或反之亦然; DO是双向的,一旦建立连接)的通信的语义与进程间通信非常相似。换句话说,一旦你有一个对远程对象的引用(真正的一个 NSDistantObject 它充当连接的另一端的对象的代理),你的客户端代码可以像远程对象一样向远程对象发送消息:

DO handles asynchronous network details for you (all your client updates could happen on a single thread). In addition the semantics of communication with a remote object (client to server or visa versa; DO is bidirectional once the connection is established) are very similar to in-process communication. In other words, once you have a reference to the remote object (really an NSDistantObject which acts as a proxy to the object on the other end of the connection), your client code can send messages to the remote object as if it were local:

[remoteServer update:client];

从客户或

[[remoteClientList objectAtIndex:i] update:server];

。我将在读取分布式对象编程指南

使用DO的缺点是你绑定到Cocoa;很难编写非Cocoa客户端或使用Distirbuted对象进行通信的服务器。如果有机会你可能想要有非Cocoa客户端或服务器实现,你不应该使用DO。在这种情况下,我会推荐一些简单的很多跨平台和语言支持。 REST式的API是一个很好的选择。请查看Cocoa 网址加载系统文档,了解如何实现HTTP请求和响应。查看Apple的 CocoaHTTPServer 示例代码或code.google.com项目的同名有关在您的Cocoa代码中实现HTTP服务器的信息。

The downside of using DO is that you are tied to Cocoa; it will be very difficult to write a non-Cocoa client or server that communicates using Distirbuted Objects. If there's a chance you may want to have non-Cocoa client or server implementations, you should not use DO. In this case, I would recommend something simple with a lot of cross-platform and language support. A REST-style API over HTTP is a good option. Have a look at the Cocoa URL Loading System documentation for info on how to implement HTTP requests and responses. Have a look at Apple's CocoaHTTPServer example code or a code.google.com project of the same name for info on implementing an HTTP server in your Cocoa code.

作为最后一个选项,您可以查看Cocoa Stream Programming Guide 如果您想实现自己的网络协议。 NSStream 的子类将允许您侦听网络套接字并处理从该套接字到/的异步读/写。很多人为此目的使用 AsyncSocket 。它包装(低级)CFStream和CFSocket,并使编写网络代码更容易一些。

As a very last option, you can take a look at the Cocoa Stream Programming Guide if you want to implement your own network protocol. NSStream's subclasses will let you listen on a network socket and handle asynchronous reads/writes to/from that socket. A lot of people use AsyncSocket for this purpose. It wraps the (lower-level) CFStream and CFSocket and makes writing network code somewhat easier.

这篇关于Objective-C网络 - 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆