Skip to content

Commit

Permalink
Update web socket demo
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Jan 1, 2022
1 parent 8c4c449 commit 615ab21
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
12 changes: 9 additions & 3 deletions Sources/Heze/Socket/HezeSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler {
return nil
}

open class func makeInstance(context: HezeContext) -> HezeSocketHandler? {
let instance = Self.init() as HezeSocketHandler
instance.bindContext(context)
return instance
}

open override func handleThenComplete(_ req: HTTPRequest, _ res: HTTPResponse) {
request = req
defer {
Expand All @@ -27,14 +33,14 @@ open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler {
handleInvaild(req, res)
return
}
WebSocketHandler(handlerProducer: { (request, protocols) -> WebSocketSessionHandler? in
WebSocketHandler(handlerProducer: { (request, protocols) in
if let socketProtocol = self.socketProtocol {
guard protocols.contains(socketProtocol) else {
return nil
}
return self
return Self.makeInstance(context: self.context)
} else {
return self
return Self.makeInstance(context: self.context)
}
}).handleRequest(request: req, response: res)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/HezeDemo/Base/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AppDelegate: HezeAppDelegate {
],

"socket": [
.get: HezeSocketClient.meta,
.get: RepeatSocketClient.meta,
]
]
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/HezeDemo/Socket/RepeatSocketClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// RepeatSocketClient.swift
// HezeDemo
//
// Created by Yuu Zheng on 2022/1/1.
//

import Foundation
import Heze

var RepeatSocketClientStorage: HezeSocketHandler?

class RepeatSocketClient: HezeSocketClient {

override func receiveBytes(_ bytes: [UInt8]) -> HezeResponsable? {
return bytes
}

override class func makeInstance(context: HezeContext) -> HezeSocketHandler? {
if RepeatSocketClientStorage == nil {
RepeatSocketClientStorage = super.makeInstance(context: context)
}
return RepeatSocketClientStorage
}
}

0 comments on commit 615ab21

Please sign in to comment.