README.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. SocketRocket Objective-C WebSocket Client (beta)
  2. ================================================
  3. A conforming WebSocket (`RFC 6455 <http://tools.ietf.org/html/rfc6455>`_)
  4. client library.
  5. `Test results for SocketRocket here <http://square.github.io/SocketRocket/results/>`_.
  6. You can compare to what `modern browsers look like here
  7. <http://tavendo.com/autobahn/testsuite/report/clients/index.html>`_.
  8. SocketRocket currently conforms to all ~300 of `Autobahn
  9. <http://autobahn.ws/testsuite/>`_'s fuzzing tests (aside from
  10. two UTF-8 ones where it is merely *non-strict*. tests 6.4.2 and 6.4.4)
  11. Features/Design
  12. ---------------
  13. - TLS (wss) support. It uses CFStream so we get this for *free*
  14. - Uses NSStream/CFNetworking. Earlier implementations used ``dispatch_io``,
  15. however, this proved to be make TLS nearly impossible. Also I wanted this to
  16. work in iOS 4.x. (SocketRocket only supports 5.0 and above now)
  17. - Uses ARC. It uses the 4.0 compatible subset (no weak refs).
  18. - Seems to perform quite well
  19. - Parallel architecture. Most of the work is done in background worker queues.
  20. - Delegate-based. Had older versions that could use blocks too, but I felt it
  21. didn't blend well with retain cycles and just objective C in general.
  22. Changes
  23. -------
  24. v0.3.1-beta2 - 2013-01-12
  25. `````````````````````````
  26. - Stability fix for ``closeWithCode:reason:`` (Thanks @michaelpetrov!)
  27. - Actually clean up the NSStreams and remove them from their runloops
  28. - ``_SRRunLoopThread``'s ``main`` wasn't correctly wrapped with
  29. ``@autoreleasepool``
  30. v0.3.1-beta1 - 2013-01-12
  31. `````````````````````````
  32. - Cleaned up GCD so OS_OBJECT_USE_OBJC_RETAIN_RELEASE is optional
  33. - Removed deprecated ``dispatch_get_current_queue`` in favor of ``dispatch_queue_set_specific`` and ``dispatch_get_specific``
  34. - Dropping support for iOS 4.0 (it may still work)
  35. Installing (iOS)
  36. ----------------
  37. There's a few options. Choose one, or just figure it out
  38. - You can copy all the files in the SocketRocket group into your app.
  39. - Include SocketRocket as a subproject and use libSocketRocket
  40. If you do this, you must add -ObjC to your "other linker flags" option
  41. - For OS X you will have to repackage make a .framework target. I will take
  42. contributions. Message me if you are interested.
  43. Depending on how you configure your project you may need to ``#import`` either
  44. ``<SocketRocket/SRWebSocket.h>`` or ``"SRWebSocket.h"``
  45. Framework Dependencies
  46. ``````````````````````
  47. Your .app must be linked against the following frameworks/dylibs
  48. - libicucore.dylib
  49. - CFNetwork.framework
  50. - Security.framework
  51. - Foundation.framework
  52. Installing (OS X)
  53. -----------------
  54. SocketRocket now has (64-bit only) OS X support. ``SocketRocket.framework``
  55. inside Xcode project is for OS X only. It should be identical in function aside
  56. from the unicode validation. ICU isn't shipped with OS X which is what the
  57. original implementation used for unicode validation. The workaround is much
  58. more rudimentary and less robust.
  59. 1. Add SocketRocket.xcodeproj as either a subproject of your app or in your workspace.
  60. 2. Add ``SocketRocket.framework`` to the link libraries
  61. 3. If you don't have a "copy files" step for ``Framework``, create one
  62. 4. Add ``SocketRocket.framework`` to the "copy files" step.
  63. API
  64. ---
  65. The classes
  66. ``SRWebSocket``
  67. ```````````````
  68. The Web Socket.
  69. .. note:: ``SRWebSocket`` will retain itself between ``-(void)open`` and when it
  70. closes, errors, or fails. This is similar to how ``NSURLConnection`` behaves.
  71. (unlike ``NSURLConnection``, ``SRWebSocket`` won't retain the delegate)
  72. What you need to know
  73. .. code-block:: objective-c
  74. @interface SRWebSocket : NSObject
  75. // Make it with this
  76. - (id)initWithURLRequest:(NSURLRequest *)request;
  77. // Set this before opening
  78. @property (nonatomic, assign) id <SRWebSocketDelegate> delegate;
  79. - (void)open;
  80. // Close it with this
  81. - (void)close;
  82. // Send a UTF8 String or Data
  83. - (void)send:(id)data;
  84. @end
  85. ``SRWebSocketDelegate``
  86. ```````````````````````
  87. You implement this
  88. .. code-block:: objective-c
  89. @protocol SRWebSocketDelegate <NSObject>
  90. - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  91. @optional
  92. - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  93. - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  94. - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  95. @end
  96. Known Issues/Server Todo's
  97. --------------------------
  98. - Needs auth delegates (like in NSURLConnection)
  99. - Move the streams off the main runloop (most of the work is backgrounded uses
  100. GCD, but I just haven't gotten around to moving it off the main loop since I
  101. converted it from dispatch_io)
  102. - Re-implement server. I removed an existing implementation as well because it
  103. wasn't being used and I wasn't super happy with the interface. Will revisit
  104. this.
  105. - Separate framer and client logic. This will make it nicer when having a
  106. server.
  107. Testing
  108. -------
  109. Included are setup scripts for the python testing environment. It comes
  110. packaged with vitualenv so all the dependencies are installed in userland.
  111. To run the short test from the command line, run::
  112. make test
  113. To run all the tests, run::
  114. make test_all
  115. The short tests don't include the performance tests. (the test harness is
  116. actually the bottleneck, not SocketRocket).
  117. The first time this is run, it may take a while to install the dependencies. It
  118. will be smooth sailing after that. After the test runs the makefile will open
  119. the results page in your browser. If nothing comes up, you failed. Working on
  120. making this interface a bit nicer.
  121. To run from the app, choose the ``SocketRocket`` target and run the test action
  122. (``cmd+u``). It runs the same thing, but makes it easier to debug. There is
  123. some serious pre/post hooks in the Test action. You can edit it to customize
  124. behavior.
  125. .. note:: Xcode only up to version 4.4 is currently supported for the test
  126. harness
  127. TestChat Demo Application
  128. -------------------------
  129. SocketRocket includes a demo app, TestChat. It will "chat" with a listening
  130. websocket on port 9900.
  131. It's a simple project. Uses storyboard. Storyboard is sweet.
  132. TestChat Server
  133. ```````````````
  134. We've included a small server for the chat app. It has a simple function.
  135. It will take a message and broadcast it to all other connected clients.
  136. We have to get some dependencies. We also want to reuse the virtualenv we made
  137. when we ran the tests. If you haven't run the tests yet, go into the
  138. SocketRocket root directory and type::
  139. make test
  140. This will set up your `virtualenv <https://pypi.python.org/pypi/virtualenv>`_.
  141. Now, in your terminal::
  142. source .env/bin/activate
  143. pip install git+https://github.com/tornadoweb/tornado.git
  144. In the same terminal session, start the chatroom server::
  145. python TestChatServer/py/chatroom.py
  146. There's also a Go implementation (with the latest weekly) where you can::
  147. cd TestChatServer/go
  148. go run chatroom.go
  149. Chatting
  150. ````````
  151. Now, start TestChat.app (just run the target in the Xcode project). If you had
  152. it started already you can hit the refresh button to reconnect. It should say
  153. "Connected!" on top.
  154. To talk with the app, open up your browser to `http://localhost:9000 <http://localhost:9000>`_ and
  155. start chatting.
  156. WebSocket Server Implementation Recommendations
  157. -----------------------------------------------
  158. SocketRocket has been used with the following libraries:
  159. - `Tornado <https://github.com/facebook/tornado>`_
  160. - Go's `WebSocket package <https://godoc.org/golang.org/x/net/websocket>`_ or Gorilla's `version <http://www.gorillatoolkit.org/pkg/websocket>`_
  161. - `Autobahn <http://tavendo.com/autobahn/testsuite.html>`_ (using its fuzzing
  162. client)
  163. The Tornado one is dirt simple and works like a charm. (`IPython notebook
  164. <http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html>`_ uses it
  165. too). It's much easier to configure handlers and routes than in
  166. Autobahn/twisted.
  167. As far as Go's goes, it works in my limited testing. I much prefer go's
  168. concurrency model as well. Try it! You may like it.
  169. It could use some more control over things such as pings, etc., but I
  170. am sure it will come in time.
  171. Autobahn is a great test suite. The Python server code is good, and conforms
  172. well (obviously). However for me, twisted would be a deal-breaker for writing
  173. something new. I find it a bit too complex and heavy for a simple service. If
  174. you are already using twisted though, Autobahn is probably for you.
  175. Contributing
  176. ------------
  177. We’re glad you’re interested in SocketRocket, and we’d love to see where you take it. Please read our `contributing guidelines <https://github.com/square/SocketRocket/blob/master/Contributing.md>`_ prior to submitting a Pull Request.