Latest 1.0.9
Homepage https://github.com/mylcode/MCWebSocket
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors

CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

MCWebSocket is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'MCWebSocket'

实现步骤

一、认证握手

1、客户端发起连接请求

GET ws://127.0.0.1:1688HTTP/1.1
Host: 127.0.0.1:1688
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 8nR/IHznszs77IXrvoSIKQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

2、服务端响应(握手成功)可以开始通讯

HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: DM03CzPOkPMsGtTWLXF8UGXURTU=

Sec-WebSocket-Accept就是把客户端上报的Sec-WebSocket-Key加上258EAFA5-E914-47DA-95CA-C5AB0DC85B11后做sha1计算,再base64之后返回给客户端。

二、数据传输

Websocket协议通过序列化的数据帧传输数据。数据封包协议中定义了opcode、payload length、Payload data等字段。

其中要求:

  • 客户端向服务器传输的数据帧必须进行掩码处理:服务器若接收到未经过掩码处理的数据帧,则必须主动关闭连接。
  • 服务器向客户端传输的数据帧一定不能进行掩码处理。客户端若接收到经过掩码处理的数据帧,则必须主动关闭连接。

针对上情况,发现错误的一方可向对方发送关闭帧(状态码是1002,表示协议错误),以关闭连接。

具体数据帧格式如下图:
Base Framing Protocl

主要用到位运算:亦或、逻辑与、逻辑或

License

MCWebSocket is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "MCWebSocket",
    "version": "1.0.9",
    "summary": "MCWebSocket is a objective-c implementation of the WebSocket protocol.",
    "description": "websocket protocol rfc4655(https://tools.ietf.org/html/rfc6455).ntu5b9eu73b0u4e86websocketu534fu8baeuff0cu987au4fbfu5b66u4e60u4e0buff0cu4e00u70b9u70b9u5199u5427uff0cu8fd8u6709u5f88u591au6ca1u6709u5b8cu6210u7684u5730u65b9uff0cu4e3bu8981u82f1u8bedu592au5deeu4e86u5f88u591au8d44u6599u90fdu770bu4e0du61c2",
    "homepage": "https://github.com/mylcode/MCWebSocket",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "mylcode": "[email protected]"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/mylcode/MCWebSocket.git",
        "tag": "1.0.9"
    },
    "default_subspecs": "standard",
    "requires_arc": true,
    "pod_target_xcconfig": {
        "GCC_PREPROCESSOR_DEFINITIONS": "XCODECOLORS=1"
    },
    "subspecs": [
        {
            "name": "standard",
            "source_files": "MCWebSocket/Classes/*",
            "exclude_files": "MCWebSocket/Classes/DB/*",
            "dependencies": {
                "CocoaAsyncSocket": [
                    "~> 7.6.0"
                ]
            }
        },
        {
            "name": "DB",
            "source_files": "MCWebSocket/Classes/DB/*",
            "dependencies": {
                "MCWebSocket/standard": [],
                "FMDB": [],
                "MCJSONKit": []
            }
        }
    ]
}

Pin It on Pinterest

Share This