1 /* 2 * MsgTrans - Message Transport Framework for DLang. Based on TCP, WebSocket, UDP transmission protocol. 3 * 4 * Copyright (C) 2019 HuntLabs 5 * 6 * Website: https://www.msgtrans.org 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module msgtrans.channel.websocket.WebSocketTransportSession; 13 14 import msgtrans.channel.TransportSession; 15 import msgtrans.MessageBuffer; 16 import msgtrans.Packet; 17 18 import hunt.http.WebSocketConnection; 19 import hunt.logging.ConsoleLogger; 20 import hunt.net; 21 22 /** 23 * 24 */ 25 class WebsocketTransportSession : TransportSession { 26 private WebSocketConnection _conn = null; 27 28 this(long id, WebSocketConnection connection) { 29 _conn = connection; 30 super(id); 31 } 32 33 override Object getAttribute(string key) { 34 return _conn.getAttribute(key); 35 } 36 37 override void setAttribute(string key, Object value) { 38 _conn.setAttribute(key, value); 39 } 40 41 override bool containsAttribute(string key) { 42 return _conn.containsAttribute(key); 43 } 44 45 override void send(MessageBuffer message) { 46 warningf("isConnected: %s", _conn.isConnected()); 47 if (_conn.isConnected()) { 48 ubyte[][] buffers = Packet.encode(message); 49 foreach(ubyte[] data; buffers) { 50 _conn.sendData(cast(byte[])data); 51 } 52 } 53 } 54 55 override void close() { 56 if (_conn !is null && _conn.getIOState().isOpen()) { 57 _conn.close(); 58 } 59 } 60 61 override bool isConnected() { 62 return _conn.isConnected(); 63 } 64 }