1 module hunt.imf.protocol.websocket.WsConnectionEventHandler;
2 
3 import hunt.imf.ConnectionEventBaseHandler;
4 import hunt.imf.ConnectBase;
5 import hunt.imf.MessageBuffer;
6 import hunt.http.server.WebSocketHandler;
7 import hunt.http.codec.http.model;
8 import hunt.http.codec.http.stream;
9 import hunt.http.codec.websocket.frame;
10 import hunt.http.codec.websocket.model;
11 import hunt.http.codec.websocket.stream.WebSocketConnection;
12 import hunt.http.codec.websocket.stream.IOState;
13 import hunt.imf.protocol.websocket.WsConnection;
14 import hunt.Byte;
15 import hunt.util.Serialize;
16 import hunt.String;
17 import hunt.logging;
18 
19 
20 class WsConnectionEventHandler : WebSocketHandler {
21 
22     alias ConnCallBack = void delegate(ConnectBase connection);
23     alias CloseConnCallBack = void delegate(ConnectBase connection );
24     alias MsgCallBack = void delegate(WebSocketConnection connection ,Frame message);
25 
26     this(string attribute)
27     {
28         _attribute = attribute;
29     }
30 
31     override
32     void onConnect(WebSocketConnection webSocketConnection) {
33         import hunt.net.Connection;
34         if (_onConnection !is null)
35         {
36             webSocketConnection.getTcpConnection.setAttribute(SESSION.PROTOCOL,new String(_attribute));
37             WsConnection conn = new WsConnection(webSocketConnection);
38             _onConnection(conn);
39         }
40         webSocketConnection.onClose((HttpConnection conn){
41             conn.getTcpConnection().setState(ConnectionState.Closed);
42             if (_onClosed !is null)
43             {
44                 _onClosed(new WsConnection(webSocketConnection));
45             }
46         });
47     }
48 
49     override
50     void onError(Exception t, WebSocketConnection connection) {
51 
52     }
53 
54     override
55     void onFrame(Frame frame, WebSocketConnection conn) {
56 
57         FrameType type = frame.getType();
58         switch (type) {
59             case FrameType.TEXT:
60             {
61                 break;
62             }
63             case FrameType.BINARY:
64             {
65                 BinaryFrame binFrame = cast(BinaryFrame) frame;
66                 ConnectBase.dispatchMessage(new WsConnection(conn),MessageBuffer.decode(cast(ubyte[])binFrame.getPayload().getRemaining()));
67                 break;
68             }
69             default:
70                 break;
71         }
72     }
73 
74 
75     void setOnConnection(ConnCallBack callback)
76     {
77         _onConnection = callback;
78     }
79 
80 
81     void setOnClosed(CloseConnCallBack callback)
82     {
83         _onClosed = callback;
84     }
85 
86 
87     void setOnMessage(MsgCallBack callback)
88     {
89         _onMessage = callback;
90     }
91 
92     private
93     {
94         string _attribute = null;
95         ConnCallBack _onConnection = null;
96         CloseConnCallBack _onClosed = null;
97         MsgCallBack _onMessage = null;
98     }
99 }
100