Skip to content

Commit

Permalink
重连时发送没成功发送的内容
Browse files Browse the repository at this point in the history
  • Loading branch information
zanjie1999 committed May 13, 2021
1 parent 671d6e7 commit 1936c10
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions tcp2ws.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Tcp over WebSocket (tcp2ws)
// 基于ws的内网穿透工具
// Sparkle 20210430
// v3.3
// v3.4

package main

Expand All @@ -23,6 +23,7 @@ type tcp2wsSparkle struct {
wsConn *websocket.Conn
uuid string
del bool
buf []byte
}

var (
Expand Down Expand Up @@ -85,6 +86,8 @@ func ReadTcp2Ws(uuid string) (bool) {
log.Print(uuid, " ws write err: ", err)
// tcpConn.Close()
wsConn.Close()
// save send error buf
connMap[uuid].buf = buf[:length]
return true
}
// if !isServer {
Expand Down Expand Up @@ -168,6 +171,11 @@ func RunServer(wsConn *websocket.Conn) {
tcpConn = conn.tcpConn
conn.wsConn.Close()
conn.wsConn = wsConn
if conn.buf != nil {
// send error buf
wsConn.WriteMessage(websocket.TextMessage, connMap[uuid].buf)
conn.buf = nil
}
}
}
}
Expand All @@ -183,7 +191,7 @@ func RunServer(wsConn *websocket.Conn) {
if uuid != "" {
// save
conn_num += 1
connMap[uuid] = &tcp2wsSparkle {tcpConn, wsConn, uuid, false}
connMap[uuid] = &tcp2wsSparkle {tcpConn, wsConn, uuid, false, nil}
}

go ReadTcp2Ws(uuid)
Expand Down Expand Up @@ -229,12 +237,17 @@ func RunClient(tcpConn net.Conn, uuid string) {
if tcpConn != nil {
// save
conn_num += 1
connMap[uuid] = &tcp2wsSparkle {tcpConn, wsConn, uuid, false}
connMap[uuid] = &tcp2wsSparkle {tcpConn, wsConn, uuid, false, nil}
} else {
// update
if _, haskey := connMap[uuid]; haskey {
connMap[uuid].wsConn.Close()
connMap[uuid].wsConn = wsConn
if conn, haskey := connMap[uuid]; haskey {
conn.wsConn.Close()
conn.wsConn = wsConn
if conn.buf != nil {
// send error buf
wsConn.WriteMessage(websocket.TextMessage, conn.buf)
conn.buf = nil
}
}
}

Expand Down

0 comments on commit 1936c10

Please sign in to comment.