forked from mbk-lab/rui_orig
2
0
Fork 0

Added writeMutex to wsBridge

This commit is contained in:
Alexei Anoshenko 2024-02-10 12:25:01 +03:00
parent 5354ec6ea1
commit d07b24c953
1 changed files with 16 additions and 5 deletions

View File

@ -17,6 +17,7 @@ type wsBridge struct {
answer map[int]chan DataObject answer map[int]chan DataObject
answerID int answerID int
answerMutex sync.Mutex answerMutex sync.Mutex
writeMutex sync.Mutex
closed bool closed bool
buffer strings.Builder buffer strings.Builder
canvasBuffer strings.Builder canvasBuffer strings.Builder
@ -51,7 +52,8 @@ func CreateSocketBridge(w http.ResponseWriter, req *http.Request) webBridge {
func (bridge *wsBridge) close() { func (bridge *wsBridge) close() {
bridge.closed = true bridge.closed = true
bridge.conn.Close() defer bridge.conn.Close()
bridge.conn = nil
} }
func (bridge *wsBridge) startUpdateScript(htmlID string) bool { func (bridge *wsBridge) startUpdateScript(htmlID string) bool {
@ -60,7 +62,7 @@ func (bridge *wsBridge) startUpdateScript(htmlID string) bool {
} }
buffer := allocStringBuilder() buffer := allocStringBuilder()
bridge.updateScripts[htmlID] = buffer bridge.updateScripts[htmlID] = buffer
buffer.WriteString("var element = document.getElementById('") buffer.WriteString("let element = document.getElementById('")
buffer.WriteString(htmlID) buffer.WriteString(htmlID)
buffer.WriteString("');\nif (element) {\n") buffer.WriteString("');\nif (element) {\n")
return true return true
@ -171,7 +173,11 @@ func (bridge *wsBridge) callFunc(funcName string, args ...any) bool {
if ProtocolInDebugLog { if ProtocolInDebugLog {
DebugLog("Run func: " + funcText) DebugLog("Run func: " + funcText)
} }
if err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(funcText)); err != nil {
bridge.writeMutex.Lock()
err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(funcText))
bridge.writeMutex.Unlock()
if err != nil {
ErrorLog(err.Error()) ErrorLog(err.Error())
return false return false
} }
@ -271,7 +277,7 @@ func (bridge *wsBridge) updateCanvasProperty(property string, value any) {
func (bridge *wsBridge) createCanvasVar(funcName string, args ...any) any { func (bridge *wsBridge) createCanvasVar(funcName string, args ...any) any {
bridge.canvasVarNumber++ bridge.canvasVarNumber++
result := canvasVar{name: fmt.Sprintf("v%d", bridge.canvasVarNumber)} result := canvasVar{name: fmt.Sprintf("v%d", bridge.canvasVarNumber)}
bridge.canvasBuffer.WriteString("\nvar ") bridge.canvasBuffer.WriteString("\nlet ")
bridge.canvasBuffer.WriteString(result.name) bridge.canvasBuffer.WriteString(result.name)
bridge.canvasBuffer.WriteString(" = ctx.") bridge.canvasBuffer.WriteString(" = ctx.")
bridge.canvasBuffer.WriteString(funcName) bridge.canvasBuffer.WriteString(funcName)
@ -335,11 +341,13 @@ func (bridge *wsBridge) canvasFinish() {
DebugLog("Run script:") DebugLog("Run script:")
DebugLog(script) DebugLog(script)
} }
bridge.writeMutex.Lock()
if bridge.conn == nil { if bridge.conn == nil {
ErrorLog("No connection") ErrorLog("No connection")
} else if err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(script)); err != nil { } else if err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(script)); err != nil {
ErrorLog(err.Error()) ErrorLog(err.Error())
} }
bridge.writeMutex.Unlock()
} }
func (bridge *wsBridge) readMessage() (string, bool) { func (bridge *wsBridge) readMessage() (string, bool) {
@ -363,7 +371,10 @@ func (bridge *wsBridge) writeMessage(script string) bool {
ErrorLog("No connection") ErrorLog("No connection")
return false return false
} }
if err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(script)); err != nil { bridge.writeMutex.Lock()
err := bridge.conn.WriteMessage(websocket.TextMessage, []byte(script))
bridge.writeMutex.Unlock()
if err != nil {
ErrorLog(err.Error()) ErrorLog(err.Error())
return false return false
} }