-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package websocket | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// WebSocket upgrader | ||
var upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { | ||
// Allow all connections for this example. In production, restrict origins. | ||
return true | ||
}, | ||
} | ||
|
||
func Conn(c *gin.Context) { | ||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
log.Println("Failed to upgrade to WebSocket:", err) | ||
return | ||
} | ||
|
||
defer conn.Close() | ||
|
||
log.Println("WebSocket connection established") | ||
|
||
for { | ||
// Read a message | ||
messageType, msg, err := conn.ReadMessage() | ||
if err != nil { | ||
log.Println("Error reading message:", err) | ||
break | ||
} | ||
|
||
log.Printf("Received message: %s\n", msg) | ||
|
||
// Echo the message back to the client | ||
err = conn.WriteMessage(messageType, []byte("Echo: "+string(msg))) | ||
if err != nil { | ||
log.Println("Error writing message:", err) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters