LoginSignup
11
8

More than 5 years have passed since last update.

KituraでWebSocketを動かす

Posted at

KituraでWebSocketを動かす

はじめに

Twitterを眺めていたら、

KituraでWebSockerが使えるようになったというツイートを見かけたので、記事を書いてみることにしました。

環境

Mac OS X El Capitan 10.11.6
Swift 3.0.2

プロジェクトのセットアップ

$ mkdir SocketServer
$ cd SocketServer
$ swift package init --type executable
SocketServer
├── Package.swift
├── Sources
│   └── main.swift
└── Tests

上記のようにファイルが生成されます。

パッケージの追加

Package.swift
import PackageDescription

let package = Package(
    name: "SocketServer",
    dependencies: [
        .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 5),
        .Package(url: "https://github.com/IBM-Swift/Kitura-WebSocket", majorVersion: 0, minor: 5),
        .Package(url: "https://github.com/IBM-Swift/HeliumLogger.git", majorVersion: 1, minor: 5)
    ]
)

以下のフレームワークをインポートしました。

  • Kitura
    HTTP Server用のWebフレームワーク

  • Kitura-WebSocket
    Kitura用のWebSocketフレームワーク

  • HeliumLogger
    Swift用のロギングフレームワーク

ここでいちど$ swift buildを実行することで、リポジトリからのクローンと依存ライブラリのコンパイルを行います。

$ swift build
・・・
Compile Swift Module 'Socket' (3 sources)
Compile Swift Module 'LoggerAPI' (1 sources)
Compile Swift Module 'Cryptor' (10 sources)
Compile Swift Module 'KituraTemplateEngine' (1 sources)
Compile Swift Module 'SwiftyJSON' (2 sources)
Compile Swift Module 'HeliumLogger' (2 sources)
Compile Swift Module 'SSLService' (1 sources)
Compile CHTTPParser utils.c
Compile CHTTPParser http_parser.c
Linking CHTTPParser
Compile Swift Module 'KituraNet' (35 sources)
Compile Swift Module 'KituraWebSocket' (9 sources)
Compile Swift Module 'Kitura' (42 sources)
Compile Swift Module 'SocketServer' (1 sources)
Linking ./.build/debug/SocketServer

WebSocketサーバの実装

Sources/main.swift
import Foundation

import Kitura
import KituraWebSocket

import HeliumLogger

HeliumLogger.use()

let router = Router()

WebSocket.register(service: MyService(), onPath: "websocket")

Kitura.addHTTPServer(onPort: 8090, with: router)

Kitura.run()
Sources/MyService.swift
import Foundation

import KituraWebSocket
import LoggerAPI

class MyService: WebSocketService {
    public func connected(connection: WebSocketConnection) {}

    public func disconnected(connection: WebSocketConnection, reason: WebSocketCloseReasonCode) {}

    public func received(message: Data, from: WebSocketConnection) {
        from.send(message: message)
    }
    public func received(message: String, from: WebSocketConnection) {
        Log.info("Got message \(message)... sending it back")
        from.send(message: message)
    }
}

今回は、8090番ポートに対してwebsocketというルーティングを追加しました。
WebSocketサーバに接続してテキストを送信すると、送信した内容をそのまま返信するようなオウム返しサービスになっています。

ソースのコンパイル

$ swift build
Compile Swift Module 'SocketServer' (2 sources)
Linking ./.build/debug/SocketServer

実行

$ .build/debug/SocketServer
・・・
[2017-01-20T14:32:12.158+09:00] [VERBOSE] [Router.swift:67 init(mergeParameters:)] Router initialized
[2017-01-20T14:32:12.170+09:00] [VERBOSE] [Kitura.swift:70 run()] Starting Kitura framework...

[2017-01-20T14:32:12.170+09:00] [VERBOSE] [Kitura.swift:80 start()] Starting an HTTP Server on port 8090...
[2017-01-20T14:32:12.171+09:00] [INFO] [HTTPServer.swift:85 listen(on:)] Listening on port 8090

HeliumLoggerを使用したので、上記のようなログが出力されています。

以下のURIでWebSocketサーバが立っているので、
ws://localhost:8090/websocket

下記のようなWebSocketクライアントを使用することで、接続確認ができます。
http://www.websocket.org/echo.html

参考

あとがき

初めてQiitaの記事を書いてみました。
Kitura自体、IBMが公開した去年の2月に少し触ってやめてしまっていたのですが、久しぶりに触ってみると、いろいろ機能が追加されてたり、だいぶ使いやすくなっていて驚いています。
これからしばらく、Kituraを触りながら記事を書いていこうと思います。

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8