now using in wstotcp as well.
This commit is contained in:
parent
6b9dc2bfbe
commit
c5f31f660c
@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cidebug/pkg/iowrappers"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -33,11 +35,12 @@ func main() {
|
|||||||
|
|
||||||
func handleWebSocket(w http.ResponseWriter, r *http.Request, tcpConn net.Conn) {
|
func handleWebSocket(w http.ResponseWriter, r *http.Request, tcpConn net.Conn) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
wsConn := iowrappers.NewWebSocketConn(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error upgrading to WebSocket:", err)
|
log.Println("Error upgrading to WebSocket:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer wsConn.Close()
|
||||||
|
|
||||||
waitChannel := make(chan bool)
|
waitChannel := make(chan bool)
|
||||||
|
|
||||||
@ -46,17 +49,9 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request, tcpConn net.Conn) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
waitChannel <- true
|
waitChannel <- true
|
||||||
}()
|
}()
|
||||||
for {
|
_, err := io.Copy(tcpConn, wsConn)
|
||||||
_, message, err := conn.ReadMessage()
|
if err != nil {
|
||||||
if err != nil {
|
log.Printf("Error occurred %v", err)
|
||||||
log.Println("Error reading WebSocket message:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_, err = tcpConn.Write(message)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error sending message to TCP server:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -64,18 +59,9 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request, tcpConn net.Conn) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
waitChannel <- true
|
waitChannel <- true
|
||||||
}()
|
}()
|
||||||
for {
|
_, err := io.Copy(wsConn, tcpConn)
|
||||||
buffer := make([]byte, 1024)
|
if err != nil {
|
||||||
n, err := tcpConn.Read(buffer)
|
log.Printf("Error occurred %v", err)
|
||||||
if err != nil {
|
|
||||||
log.Println("Error reading from TCP server:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = conn.WriteMessage(websocket.TextMessage, buffer[:n])
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error writing WebSocket message:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user