diff --git a/Sources/Heze/Socket/HezeSocketHandler.swift b/Sources/Heze/Socket/HezeSocketHandler.swift index d828a17..7f95be7 100644 --- a/Sources/Heze/Socket/HezeSocketHandler.swift +++ b/Sources/Heze/Socket/HezeSocketHandler.swift @@ -12,6 +12,8 @@ import PerfectWebSockets open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler { public var socket: WebSocket? = nil + public var remoteHost: String? + public var remotePort: UInt16? public var buffer = [UInt8]() open var socketProtocol: String? { @@ -34,14 +36,26 @@ open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler { return } WebSocketHandler(handlerProducer: { (request, protocols) in + let handler: HezeSocketHandler? if let socketProtocol = self.socketProtocol { guard protocols.contains(socketProtocol) else { return nil } - return type(of: self).create(context: self.context) + #if os(macOS) + handler = Self.create(context: self.context) + #else + handler = type(of: self).create(context: self.context) + #endif } else { - return type(of: self).create(context: self.context) + #if os(macOS) + handler = Self.create(context: self.context) + #else + handler = type(of: self).create(context: self.context) + #endif } + handler?.remoteHost = req.remoteAddress.host + handler?.remotePort = req.remoteAddress.port + return handler }).handleRequest(request: req, response: res) }