socket.io php client,socket.io-client-swift:Socket.IO 客户端

68747470733a2f2f7472617669732d63692e6f72672f736f636b6574696f2f736f636b65742e696f2d636c69656e742d73776966742e7376673f6272616e63683d6d6173746572

Socket.IO-Client-Swift

Socket.IO-client for iOS/OS X.

Example

import SocketIO

let manager = SocketManager(socketURL: URL(string: "http://localhost:8080")!, config: [.log(true), .compress])

let socket = manager.defaultSocket

socket.on(clientEvent: .connect) {data, ack in

print("socket connected")

}

socket.on("currentAmount") {data, ack in

guard let cur = data[0] as? Double else { return }

socket.emitWithAck("canUpdate", cur).timingOut(after: 0) {data in

socket.emit("update", ["amount": cur + 2.50])

}

ack.with("Got your currentAmount", "dude")

}

socket.connect()

Objective-C Example

@import SocketIO;

NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"];

SocketManager* manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log": @YES, @"compress": @YES}];

SocketIOClient* socket = manager.defaultSocket;

[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {

NSLog(@"socket connected");

}];

[socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {

double cur = [[data objectAtIndex:0] floatValue];

[[socket emitWithAck:@"canUpdate" with:@[@(cur)]] timingOutAfter:0 callback:^(NSArray* data) {

[socket emit:@"update" with:@[@{@"amount": @(cur + 2.50)}]];

}];

[ack with:@[@"Got your currentAmount, ", @"dude"]];

}];

[socket connect];

Features

Supports socket.io 2.0+ (For socket.io 1.0 use v9.x)

Supports binary

Supports Polling and WebSockets

Supports TLS/SSL

Can be used from Objective-C

FAQS

Checkout the FAQs for commonly asked questions.

Checkout the 12to13 guide for migrating to v13+ from v12 below.

Installation

Requires Swift 4/5 and Xcode 10.x

If you need Swift 2.3 use the swift2.3 tag (Pre-Swift 4 support is no longer maintained)

If you need Swift 3.x use v11.1.3.

Swift Package Manager

Add the project as a dependency to your Package.swift:

// swift-tools-version:4.2

import PackageDescription

let package = Package(

name: "socket.io-test",

products: [

.executable(name: "socket.io-test", targets: ["YourTargetName"])

],

dependencies: [

.package(url: "https://github.com/socketio/socket.io-client-swift", .upToNextMinor(from: "15.0.0"))

],

targets: [

.target(name: "YourTargetName", dependencies: ["SocketIO"], path: "./Path/To/Your/Sources")

]

)

Then import import SocketIO.

Carthage

Add this line to your Cartfile:

github "socketio/socket.io-client-swift" ~> 15.2.0

Run carthage update --platform ios,macosx.

Add the Starscream and SocketIO frameworks to your projects and follow the usual Carthage process.

CocoaPods 1.0.0 or later

Create Podfile and add pod 'Socket.IO-Client-Swift':

use_frameworks!

target 'YourApp' do

pod 'Socket.IO-Client-Swift', '~> 15.2.0'

end

Install pods:

$ pod install

Import the module:

Swift:

import SocketIO

Objective-C:

@import SocketIO;

Detailed Example

A more detailed example can be found here

An example using the Swift Package Manager can be found here

License

MIT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值