1 module hunt.imf.protocol.websocket.WsConnection; 2 3 import hunt.http.codec.websocket.stream.WebSocketConnection; 4 import hunt.imf.ConnectBase; 5 import hunt.imf.MessageBuffer; 6 import hunt.http.codec.websocket.frame; 7 import hunt.net; 8 import hunt.http.HttpConnection; 9 import hunt.String; 10 import std.stdio; 11 12 class WsConnection : ConnectBase { 13 14 private { 15 WebSocketConnection _conn = null; 16 } 17 18 this(WebSocketConnection connection) { 19 _conn = connection; 20 } 21 22 override void sendMsg(MessageBuffer message) 23 { 24 if (_conn.getTcpConnection().isConnected()) 25 { 26 _conn.sendData(cast(byte[])message.encode()); 27 } 28 } 29 30 WebSocketConnection getConnection() 31 { 32 return _conn; 33 } 34 35 override string getProtocol() 36 { 37 return (cast(String)_conn.getTcpConnection().getAttribute(SESSION.PROTOCOL)).value; 38 } 39 40 override Connection getConnection() 41 { 42 return _conn.getTcpConnection(); 43 } 44 45 override void close() 46 { 47 if (_conn !is null && _conn.getTcpConnection().getState !is ConnectionState.Closed) 48 { 49 _conn.getTcpConnection().close(); 50 } 51 } 52 53 override bool isConnected() 54 { 55 return _conn.getTcpConnection().isConnected(); 56 } 57 } 58