diff --git a/appServer.go b/appServer.go index 7ac41c3..30f6fa5 100644 --- a/appServer.go +++ b/appServer.go @@ -169,47 +169,42 @@ func (app *application) postHandler(w http.ResponseWriter, req *http.Request) { } command := obj.Tag() + startSession := false - if session == nil { - switch command { - case "startSession": - events := make(chan DataObject, 1024) - bridge := createHttpBridge(req) - response = bridge.response - answer := "" - session, answer = app.startSession(obj, events, bridge, response) + if session == nil || command == "startSession" { + events := make(chan DataObject, 1024) + bridge := createHttpBridge(req) + response = bridge.response + answer := "" + session, answer = app.startSession(obj, events, bridge, response) - bridge.writeMessage(answer) - session.onStart() - bridge.sendResponse() - - setSessionIDCookie(w, session.ID()) - - go sessionEventHandler(session, events, bridge) - - default: - return + bridge.writeMessage(answer) + session.onStart() + if command == "session-resume" { + session.onResume() } + bridge.sendResponse() + + setSessionIDCookie(w, session.ID()) + startSession = true + + go sessionEventHandler(session, events, bridge) } - switch command { - case "startSession": + if !startSession { + switch command { + case "nop": + session.sendResponse() - case "nop": - session.sendResponse() - /* - case "disconnect": - session.onDisconnect() - return - */ - case "session-close": - session.onFinish() - session.App().removeSession(session.ID()) - return + case "session-close": + session.onFinish() + session.App().removeSession(session.ID()) + return - default: - if !session.handleAnswer(command, obj) { - session.addToEventsQueue(obj) + default: + if !session.handleAnswer(command, obj) { + session.addToEventsQueue(obj) + } } } diff --git a/app_post.js b/app_post.js index 71bd258..e557c55 100644 --- a/app_post.js +++ b/app_post.js @@ -17,59 +17,7 @@ window.onload = function() { sendMessage( sessionInfo() ); } -/* -window.onload = function() { - socketUrl = document.location.protocol == "https:" ? "wss://" : "ws://" - socketUrl += document.location.hostname - const port = document.location.port - if (port) { - socketUrl += ":" + port - } - socketUrl += window.location.pathname + "ws" - - socket = new WebSocket(socketUrl); - socket.onopen = socketOpen; - socket.onclose = socketClose; - socket.onerror = socketError; - socket.onmessage = function(event) { - window.execScript ? window.execScript(event.data) : window.eval(event.data); - }; -}; - -function socketOpen() { - sendMessage( sessionInfo() ); -} - -function socketReopen() { - sendMessage( "reconnect{session=" + sessionID + "}" ); -} - -function socketReconnect() { - if (!socket) { - socket = new WebSocket(socketUrl); - socket.onopen = socketReopen; - socket.onclose = socketClose; - socket.onerror = socketError; - socket.onmessage = function(event) { - window.execScript ? window.execScript(event.data) : window.eval(event.data); - }; - } -} - -function socketClose(event) { - console.log("socket closed") - socket = null; - if (!event.wasClean && windowFocus) { - window.setTimeout(socketReconnect, 10000); - } -} -*/ - -function socketError(error) { - console.log(error); -} - -window.onfocus = function(event) { +window.onfocus = function() { windowFocus = true - sendMessage( "session-resume{session=" + sessionID +"}" ); + sendMessage( "session-resume{}" ); } diff --git a/app_scripts.js b/app_scripts.js index af89df7..222a40e 100644 --- a/app_scripts.js +++ b/app_scripts.js @@ -5,11 +5,11 @@ window.onresize = function() { scanElementsSize(); } -window.onbeforeunload = function(event) { +window.onbeforeunload = function() { sendMessage( "session-close{session=" + sessionID +"}" ); } -window.onblur = function(event) { +window.onblur = function() { windowFocus = false sendMessage( "session-pause{session=" + sessionID +"}" ); } diff --git a/session.go b/session.go index a78deb4..31e5930 100644 --- a/session.go +++ b/session.go @@ -526,7 +526,9 @@ func (session *sessionData) htmlPropertyValue(htmlID, name string) string { func (session *sessionData) handleAnswer(command string, data DataObject) bool { switch command { case "answer": - session.bridge.answerReceived(data) + if session.bridge != nil { + session.bridge.answerReceived(data) + } case "imageLoaded": session.imageManager().imageLoaded(data) @@ -538,7 +540,9 @@ func (session *sessionData) handleAnswer(command string, data DataObject) bool { return false } - session.bridge.sendResponse() + if session.bridge != nil { + session.bridge.sendResponse() + } return true }