node.js iPhone Objective-C 套接字与 Socket.IO 的通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4951461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:51:24  来源:igfitidea点击:

iPhone Objective-C socket communication with Socket.IO

iphoneobjective-cnode.jssocketssocket.io

提问by Max

I'm developing some sort of massive multiplayer board-game. My solution is node.js socket.io on server. I need solution for implementation some sort of objective-c socket which would communicate with socket.io for sending and receiving json objects as string. i won't use webkit with websocket. Is there any possibility? server will hold state of game, iphone clients will sending action to server and receiving changes on server back.

我正在开发某种大型多人棋盘游戏。我的解决方案是服务器上的 node.js socket.io。我需要实现某种objective-c套接字的解决方案,该套接字将与socket.io通信以将json对象作为字符串发送和接收。我不会将 webkit 与 websocket 一起使用。有没有可能?服务器将保持游戏状态,iphone 客户端将向服务器发送动作并在服务器上接收更改。

回答by pkyeck

edited 2015/03/19:The Socket.IO guys created their own iOS library that supports Socket.IO v1.0+. Get it here: https://github.com/socketio/socket.io-client-swift

2015年 3 月 19 日编辑:Socket.IO 开发人员创建了自己的 iOS 库,支持 Socket.IO v1.0+。在这里获取:https: //github.com/socketio/socket.io-client-swift



old answer

旧答案

I updated fpotter's socket.io library to work with version 0.7.2 - 0.9.x.
It supports sockets and XHRpolling.

我更新了 fpotter 的 socket.io 库以使用0.7.2 - 0.9.x版本。
它支持套接字和 XHRpolling。

https://github.com/pkyeck/socket.IO-objc

https://github.com/pkyeck/socket.IO-objc

Maybe you want to check it out.

也许你想看看。

回答by Nick Weaver

Or have a look at cocoaasyncsocket:

或者看看cocoaasyncsocket

CocoaAsyncSocketsupports TCP and UDP. The AsyncSocket class is for TCP, and the AsyncUdpSocket class is for UDP. Each class is described below.

AsyncSocket is a TCP/IP socket networking library that wraps CFSocket and CFStream. It offers asynchronous operation, and a native cocoa class complete with delegate support. Here are the key features:

  • Queued non-blocking reads and writes, with optional timeouts. You tell it what to read or write, and it will call you when it's done.

  • Automatic socket acceptance. If you tell it to accept connections, it will call you with new instances of itself for each connection. You can, of course, disconnect them immediately.

  • Delegate support. Errors, connections, accepts, read completions, write completions, progress, and disconnections all result in a call to your delegate method.

  • Run-loop based, not thread based. Although you can use it on main or worker threads, you don't have to. It calls the delegate methods asynchronously using NSRunLoop. The delegate methods include a socket parameter, allowing you to distinguish between many instances.

  • Self-contained in one class. You don't need to muck around with streams or sockets. The class handles all of that.

  • Support for TCP streams over IPv4 and IPv6.

The library is public domain, originally written by Dustin Voss. Now available in a public setting to allow and encourage its continued support.

AsyncUdpSocketis a UDP/IP socket networking library that wraps CFSocket. It works almost exactly like the TCP version, but is designed specifically for UDP. This includes queued non-blocking send/receive operations, full delegate support, run-loop based, self-contained class, and support for IPv4 and IPv6.

CocoaAsyncSocket支持 TCP 和 UDP。AsyncSocket 类用于 TCP,而 AsyncUdpSocket 类用于 UDP。下面描述了每个类。

AsyncSocket 是一个 TCP/IP 套接字网络库,它封装了 CFSocket 和 CFStream。它提供异步操作,以及一个带有委托支持的原生 cocoa 类。以下是主要功能:

  • 排队的非阻塞读取和写入,具有可选的超时。你告诉它读什么或写什么,它会在完成后给你打电话。

  • 自动接受套接字。如果你告诉它接受连接,它会为每个连接调用你自己的新实例。当然,您可以立即断开它们的连接。

  • 委托支持。错误、连接、接受、读取完成、写入完成、进度和断开连接都会导致对委托方法的调用。

  • 基于运行循环,而不是基于线程。尽管您可以在主线程或工作线程上使用它,但您不必这样做。它使用 NSRunLoop 异步调用委托方法。委托方法包括一个套接字参数,允许您区分许多实例。

  • 自成一格。您不需要处理流或套接字。该类处理所有这些。

  • 支持通过 IPv4 和 IPv6 的 TCP 流。

该图书馆是公共领域,最初由达斯汀·沃斯 (Dustin Voss) 编写。现在可在公共环境中使用,以允许和鼓励其持续支持。

AsyncUdpSocket是一个封装了 CFSocket 的 UDP/IP 套接字网络库。它的工作原理几乎与 TCP 版本完全相同,但专为 UDP 设计。这包括排队的非阻塞发送/接收操作、完全委托支持、基于运行循环的自包含类以及对 IPv4 和 IPv6 的支持。

回答by raidfive

I'd suggest checking out the Apple documentation for streaming and sockets if you are planning on doing an native app. http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Streams/Streams.html

如果您打算开发本机应用程序,我建议您查看 Apple 文档中有关流媒体和套接字的信息。http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Streams/Streams.html