Skip to content

Commit

Permalink
fix issue #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Hener committed Nov 9, 2021
1 parent 430a483 commit 52cb396
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 2 additions & 3 deletions internal/myclipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package myclipboard

import (
"encoding/json"
"fmt"
"time"
)

Expand Down Expand Up @@ -33,13 +32,13 @@ func (c *Clipboard) AddEntry(con string) error {
entries = append(entries, Entry{
ID: newID,
Content: con,
Time: fmt.Sprintf("%s", time.Now().Format("Mon Jan _2 15:04:05 2006")),
Time: time.Now().Format("Mon Jan _2 15:04:05 2006"),
})
} else {
entries = append(entries, Entry{
ID: 0,
Content: con,
Time: fmt.Sprintf("%s", time.Now().Format("Mon Jan _2 15:04:05 2006")),
Time: time.Now().Format("Mon Jan _2 15:04:05 2006"),
})
}
c.Entries = entries
Expand Down
21 changes: 12 additions & 9 deletions internal/mysock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"strconv"
"time"

"github.com/gorilla/websocket"
Expand Down Expand Up @@ -37,7 +38,6 @@ const (

var (
newline = []byte{'\n'}
space = []byte{' '}
)

var wsupgrader = websocket.Upgrader{
Expand Down Expand Up @@ -94,22 +94,25 @@ func (c *Client) readPump() {
// Switch here over possible socket events and pull in handlers
switch packet.Type {
case "newEntry":
entry := string(packet.Content)
submitEntry := entry[1 : len(entry)-1]
if err := c.hub.cb.AddEntry(submitEntry); err != nil {
var entry string
if err := json.Unmarshal(packet.Content, &entry); err != nil {
log.Printf("ERROR: Error reading json packet: %+v", err)
}
if err := c.hub.cb.AddEntry(entry); err != nil {
log.Printf("Error: Error creating Clipboard entry: %+v", err)
}
c.refreshClipboard()

case "delEntry":
type delID struct {
Content int
}
var id delID
var id string
if err := json.Unmarshal(packet.Content, &id); err != nil {
log.Printf("ERROR: Error reading json packet: %+v", err)
}
if err := c.hub.cb.DeleteEntry(id.Content); err != nil {
iid, err := strconv.Atoi(id)
if err != nil {
log.Printf("ERROR: Error reading json packet: %+v", err)
}
if err := c.hub.cb.DeleteEntry(iid); err != nil {
log.Printf("ERROR: Error to delete Clipboard entry with id: %s: %+v", string(packet.Content), err)
}
c.refreshClipboard()
Expand Down

0 comments on commit 52cb396

Please sign in to comment.