Module: kamailio
Branch: master
Commit: 2f2bf30e4b30fef3849e9e83f7694af7b4f9c10e
URL:
https://github.com/kamailio/kamailio/commit/2f2bf30e4b30fef3849e9e83f7694af…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2016-12-22T16:12:41+01:00
websocket: implemented ws.ping and ws.pong rpc commands
---
Modified: src/modules/websocket/websocket.c
Modified: src/modules/websocket/ws_conn.h
Modified: src/modules/websocket/ws_frame.c
Modified: src/modules/websocket/ws_frame.h
---
Diff:
https://github.com/kamailio/kamailio/commit/2f2bf30e4b30fef3849e9e83f7694af…
Patch:
https://github.com/kamailio/kamailio/commit/2f2bf30e4b30fef3849e9e83f7694af…
---
diff --git a/src/modules/websocket/websocket.c b/src/modules/websocket/websocket.c
index af536e4..a1a23cf 100644
--- a/src/modules/websocket/websocket.c
+++ b/src/modules/websocket/websocket.c
@@ -367,9 +367,20 @@ static const char* ws_rpc_close_doc[2] = {
0
};
+static const char* ws_rpc_ping_doc[2] = {
+ "Send ping on a websocket connection by id",
+ 0
+};
+
+static const char* ws_rpc_pong_doc[2] = {
+ "Send pong on a websocket connection by id",
+ 0
+};
+
rpc_export_t ws_rpc_cmds[] = {
- {"ws.close", ws_rpc_close,
- ws_rpc_close_doc, 0},
+ {"ws.close", ws_rpc_close, ws_rpc_close_doc, 0},
+ {"ws.ping", ws_rpc_ping, ws_rpc_ping_doc, 0},
+ {"ws.pong", ws_rpc_pong, ws_rpc_pong_doc, 0},
{0, 0, 0, 0}
};
diff --git a/src/modules/websocket/ws_conn.h b/src/modules/websocket/ws_conn.h
index 7729c00..f66a97d 100644
--- a/src/modules/websocket/ws_conn.h
+++ b/src/modules/websocket/ws_conn.h
@@ -99,5 +99,4 @@ int wsconn_put(ws_connection_t *wsc);
ws_connection_t **wsconn_get_list(void);
int wsconn_put_list(ws_connection_t **list);
struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param);
-void ws_rpc_close(rpc_t* rpc, void* ctx);
#endif /* _WS_CONN_H */
diff --git a/src/modules/websocket/ws_frame.c b/src/modules/websocket/ws_frame.c
index 5f1257a..eefd510 100644
--- a/src/modules/websocket/ws_frame.c
+++ b/src/modules/websocket/ws_frame.c
@@ -1124,3 +1124,45 @@ void ws_rpc_close(rpc_t* rpc, void* ctx)
return;
}
}
+
+void ws_rpc_ping_pong(rpc_t* rpc, void* ctx, int opcode)
+{
+ unsigned int id;
+ ws_connection_t *wsc;
+ int ret = 0;
+
+ if(rpc->scan(ctx, "d", (int*)(&id))<1)
+ {
+ LM_WARN("no connection ID parameter\n");
+ rpc->fault(ctx, 500, "Invalid Parameters");
+ return;
+ }
+
+ if ((wsc = wsconn_get(id)) == NULL)
+ {
+ LM_WARN("bad connection ID parameter\n");
+ rpc->fault(ctx, 500, "Unknown connection ID");
+ return;
+ }
+
+ ret = ping_pong(wsc, opcode);
+
+ wsconn_put(wsc);
+
+ if (ret < 0)
+ {
+ LM_WARN("sending %s\n", OPCODE_PING ? "Ping" : "Pong");
+ rpc->fault(ctx, 500, str_status_error_sending.s);
+ return;
+ }
+}
+
+void ws_rpc_ping(rpc_t* rpc, void* ctx)
+{
+ ws_rpc_ping_pong(rpc, ctx, OPCODE_PING);
+}
+
+void ws_rpc_pong(rpc_t* rpc, void* ctx)
+{
+ ws_rpc_ping_pong(rpc, ctx, OPCODE_PONG);
+}
diff --git a/src/modules/websocket/ws_frame.h b/src/modules/websocket/ws_frame.h
index e5d2cb0..eefe9cb 100644
--- a/src/modules/websocket/ws_frame.h
+++ b/src/modules/websocket/ws_frame.h
@@ -30,6 +30,7 @@
#include "../../core/config.h"
#include "../../core/sr_module.h"
#include "../../core/str.h"
+#include "../../core/rpc.h"
#include "../../lib/kmi/tree.h"
#include "ws_conn.h"
@@ -80,4 +81,8 @@ int ws_close(sip_msg_t *msg);
int ws_close2(sip_msg_t *msg, char *_status, char *_reason);
int ws_close3(sip_msg_t *msg, char *_status, char *_reason, char *_con);
+void ws_rpc_close(rpc_t* rpc, void* ctx);
+void ws_rpc_ping(rpc_t* rpc, void* ctx);
+void ws_rpc_pong(rpc_t* rpc, void* ctx);
+
#endif /* _WS_FRAME_H */