Update main.go, main.go, and 2 more files...

This commit is contained in:
RamiBerm 2021-05-25 10:34:33 +03:00
parent 1d6f59df99
commit 6155f12015
4 changed files with 28 additions and 24 deletions

View File

@ -108,8 +108,7 @@ func pipeChannelToSocket(connection *websocket.Conn, messageDataChannel <-chan *
}
for messageData := range messageDataChannel {
websocketMessage := models.CreateWebsocketTappedEntryMessage(messageData)
marshaledData, err := json.Marshal(websocketMessage)
marshaledData, err := models.CreateWebsocketTappedEntryMessage(messageData)
if err != nil {
fmt.Printf("error converting message to json %s, (%v,%+v)\n", err, err, err)
continue

View File

@ -120,8 +120,7 @@ func saveHarToDb(entry *har.Entry, sender string) {
database.GetEntriesTable().Create(&mizuEntry)
baseEntry := utils.GetResolvedBaseEntry(mizuEntry)
messageToSend := models.CreateBaseEntryWebSocketMessage(&baseEntry)
baseEntryBytes, _ := json.Marshal(&messageToSend)
baseEntryBytes, _ := models.CreateBaseEntryWebSocketMessage(&baseEntry)
broadcastToBrowserClients(baseEntryBytes)
}

View File

@ -59,22 +59,25 @@ func (h *RoutesEventHandlers) WebSocketMessage(ep *ikisocket.EventPayload) {
err := json.Unmarshal(ep.Data, &socketMessageBase)
if err != nil {
fmt.Printf("Could not unmarshal websocket message %v\n", err)
} else if socketMessageBase.MessageType == shared.WebSocketMessageTypeTappedEntry {
var tappedEntryMessage models.WebSocketTappedEntryMessage
err := json.Unmarshal(ep.Data, &tappedEntryMessage)
if err != nil {
fmt.Printf("Could not unmarshal message of message type %s %v", socketMessageBase.MessageType, err)
} else {
h.SocketHarOutChannel <- tappedEntryMessage.Data
}
} else if socketMessageBase.MessageType == shared.WebSocketMessageTypeUpdateStatus {
var statusMessage shared.WebSocketStatusMessage
err := json.Unmarshal(ep.Data, &statusMessage)
if err != nil {
fmt.Printf("Could not unmarshal message of message type %s %v", socketMessageBase.MessageType, err)
} else {
controllers.TapStatus = statusMessage.TappingStatus
broadcastToBrowserClients(ep.Data)
} else {
switch socketMessageBase.MessageType {
case shared.WebSocketMessageTypeTappedEntry:
var tappedEntryMessage models.WebSocketTappedEntryMessage
err := json.Unmarshal(ep.Data, &tappedEntryMessage)
if err != nil {
fmt.Printf("Could not unmarshal message of message type %s %v", socketMessageBase.MessageType, err)
} else {
h.SocketHarOutChannel <- tappedEntryMessage.Data
}
case shared.WebSocketMessageTypeUpdateStatus:
var statusMessage shared.WebSocketStatusMessage
err := json.Unmarshal(ep.Data, &statusMessage)
if err != nil {
fmt.Printf("Could not unmarshal message of message type %s %v", socketMessageBase.MessageType, err)
} else {
controllers.TapStatus = statusMessage.TappingStatus
broadcastToBrowserClients(ep.Data)
}
}
}
}

View File

@ -1,6 +1,7 @@
package models
import (
"encoding/json"
"github.com/up9inc/mizu/shared"
"mizuserver/pkg/tap"
"time"
@ -60,20 +61,22 @@ type WebSocketTappedEntryMessage struct {
Data *tap.OutputChannelItem
}
func CreateBaseEntryWebSocketMessage(base *BaseEntryDetails) *WebSocketEntryMessage {
return &WebSocketEntryMessage{
func CreateBaseEntryWebSocketMessage(base *BaseEntryDetails) ([]byte, error) {
message := &WebSocketEntryMessage{
WebSocketMessageMetadata: &shared.WebSocketMessageMetadata{
MessageType: shared.WebSocketMessageTypeEntry,
},
Data: base,
}
return json.Marshal(message)
}
func CreateWebsocketTappedEntryMessage(base *tap.OutputChannelItem) *WebSocketTappedEntryMessage {
return &WebSocketTappedEntryMessage{
func CreateWebsocketTappedEntryMessage(base *tap.OutputChannelItem) ([]byte, error) {
message := &WebSocketTappedEntryMessage{
WebSocketMessageMetadata: &shared.WebSocketMessageMetadata{
MessageType: shared.WebSocketMessageTypeTappedEntry,
},
Data: base,
}
return json.Marshal(message)
}