diff --git a/.gitignore b/.gitignore index 206e3cc..d83b608 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build bin tmp +.DS_Store diff --git a/assets/js/websocket_example.js b/assets/js/websocket_example.js new file mode 100644 index 0000000..792c0e3 --- /dev/null +++ b/assets/js/websocket_example.js @@ -0,0 +1,45 @@ +let socket = null; +let msgCount = 0; + +function setupSocket() { + socket = new WebSocket("ws://localhost:5155/ws"); + + socket.addEventListener("message", (event) => { + msgCount += 1; + const msgCountElement = document.getElementById("msgCount"); + if (msgCountElement) { + msgCountElement.textContent = msgCount; + } + console.log("message: ", event.data); + }); + + socket.addEventListener("close", (event) => { + const header = document.getElementById("msg"); + header.textContent = "Connection Closed"; + console.log("connection closed"); + }); +} + +function closeConn() { + if (socket) { + socket.close(); + socket = null; + const header = document.getElementById("msg"); + header.textContent = "Connection Closed"; + console.log("connection closed"); + } +} + +function openConn() { + if (!socket || socket.readyState === WebSocket.CLOSED) { + const header = document.getElementById("msg"); + header.textContent = "Connection Open"; + console.log("connection open"); + setupSocket(); + } else { + console.log("Socket is already open or connecting."); + } +} + +// Start the first connection +setupSocket(); diff --git a/cmd/webapp/main.go b/cmd/webapp/main.go index 9672650..e60db63 100644 --- a/cmd/webapp/main.go +++ b/cmd/webapp/main.go @@ -8,14 +8,21 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" ) type DeleteSelected struct { Svgs []string `json:"svgs" binding:"required"` } +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + func main() { router := gin.Default() funcMap := template.FuncMap{ @@ -80,6 +87,23 @@ func main() { c.HTML(200, "create-icon.html", nil) }) + router.GET("/ws", func(c *gin.Context) { + conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + log.Panicln("error upgrading ws: ", err) + return + } + defer conn.Close() + for { + conn.WriteMessage(websocket.TextMessage, []byte("Hello Socket")) + time.Sleep(time.Second) + } + }) + + router.GET("/ws-example", func(c *gin.Context) { + c.HTML(200, "ws-example.html", nil) + }) + router.POST("/prompt-icon", func(c *gin.Context) { var form struct { Prompt string `form:"prompt" binding:"required"` diff --git a/go.mod b/go.mod index eecae25..e7734ea 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.26.0 // indirect github.com/goccy/go-json v0.10.5 // indirect + github.com/gorilla/websocket v1.5.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/leodido/go-urn v1.4.0 // indirect diff --git a/go.sum b/go.sum index cff88c3..c066352 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAu github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/internal/helper.go b/internal/helper.go index 0329dc7..c1454ec 100644 --- a/internal/helper.go +++ b/internal/helper.go @@ -193,7 +193,6 @@ func DownloadIcon(prompt string, outputPath string) (string, error) { } func FindNoneCollisionFileName(fileName, fileExt, path string) string { - filePath := filepath.Join(path, fileName+"."+fileExt) for fileCount := 1; FileExists(filePath); fileCount += 1 { log.Println("file exists: ", filePath) diff --git a/templates/home.html b/templates/home.html index bb6fb1e..01b7938 100644 --- a/templates/home.html +++ b/templates/home.html @@ -10,6 +10,7 @@

Pages

View Icons Create Icons + WebSocket Example diff --git a/templates/ws-example.html b/templates/ws-example.html new file mode 100644 index 0000000..f57f4bd --- /dev/null +++ b/templates/ws-example.html @@ -0,0 +1,13 @@ + + + + Websocket Test + + + +

Connection Closed

+

0

+ + + +