Skip to content

Commit e876f9f

Browse files
committed
add websocket client heartbeat
1 parent 8bcb092 commit e876f9f

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If you want to get all requests and responses about `https://www.exmample.com (1
1515
2. run commands below:
1616
```shell
1717
# run with sbt
18-
sbt "runMain Main --dns=192.168.3.3:www.example.com --jks-path=jks.jks --jks-password=123456 --websocketPort=9000"
18+
sbt "runMain Main --dns=192.168.3.3:www.example.com --jks-path=jks.jks --jks-password=123456 --viewPort=9000"
1919

2020

2121
# run with docker

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ lazy val root = project
55
.in(file("."))
66
.settings(
77
name := "log-http-proxy",
8-
version := "0.2.2",
8+
version := "0.2.3",
99
scalaVersion := scala3Version,
1010
libraryDependencies ++=
1111
tapir ++ logLib ++ configLib ++

src/main/scala/Main.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Main {
1414
@arg(doc = "jks file path") jksPath: Option[String],
1515
@arg(doc = "jks password") jksPassword: Option[String],
1616
@arg(doc = "example: 1.1.1.1,8.8.8.8") resolver: Option[String],
17-
@arg(doc = "websocket port, output log via websocket, if not set, output log to cmd") websocketPort: Option[Int],
17+
@arg(doc = "websocket port, output log via websocket, if not set, output log to cmd") viewerPort: Option[Int],
1818
): Unit = {
1919
var config = AppConfig.load()
2020

@@ -31,7 +31,7 @@ object Main {
3131
dns = dnsPairs.map((domain,ip) => DNS(ip= ip ,domain = domain)),
3232
resolver = resolver.map(_.split(',').toList).getOrElse(config.resolver),
3333
jks = jksConf.orElse(config.jks),
34-
viewerPort = websocketPort.orElse(config.viewerPort),
34+
viewerPort = viewerPort.orElse(config.viewerPort),
3535
)
3636

3737
if(config.resolver.nonEmpty) {

src/main/scala/com/timzaak/proxy/LogWebViewer.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ class LogWebViewer(certPath: Option[String])(using system: ActorSystem) {
6161
const ws_protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
6262
const ws_host = window.location.host;
6363
const socket = new WebSocket(`$${ws_protocol}//$${ws_host}/api_ws?ip=$${client_ip}`);
64-
64+
window.socket = socket
65+
let intervalId = setInterval(function() {
66+
socket.send("!");
67+
}, 30000);
6568
socket.onopen = function(event) {
6669
console.log("WebSocket connection established, please start sending request, make sure you have changed your host config");
6770
};
@@ -71,10 +74,14 @@ class LogWebViewer(certPath: Option[String])(using system: ActorSystem) {
7174
};
7275

7376
socket.onerror = function(error) {
77+
intervalId && clearInterval(intervalId);
78+
delete intervalId;
7479
console.error("WebSocket Error: ", error);
7580
};
7681

7782
socket.onclose = function(event) {
83+
intervalId && clearInterval(intervalId);
84+
delete intervalId;
7885
console.log("WebSocket connection closed");
7986
};
8087
</script>

0 commit comments

Comments
 (0)