B4J Question [WebApp] Session Exception

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi
Some time not always. b4j give me some message as following picture when i write attribute to session. o_O

WebUtils.bsMessageBox(ws,"PLC參數存檔完成")


B4X:
    Devices.plc.ip = ft10.Value
    Devices.plc.port = ft11.Value
    Devices.plc.address = ft12.Value
    Devices.plc.size = ft13.Value  
    Dim ft As Future=word1.GetProp("checked")
    Devices.plc_word1 = ft.Value
    For n1=0 To Devices.Size-1
        ft8=ws.GetElementById("inp"&(n1+1)).Getval
        Devices.Title(n1)=ft8.Value
        ft8=ws.GetElementById("used"&n1).GetProp("checked")
        Devices.used(n1)=ft8.Value
    Next  
    Devices.save
    Log("param/plc save")
    WebUtils.bsMessageBox(ws,"PLC參數存檔完成")

B4X:
Sub bsMessageBox(ws As WebSocket,msg As String)  
    ws.Session.SetAttribute("bsModal",msg)
End Sub
qa11.png
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

In my application,I have two page(websocket). One(A) is first open and always open util close page. Other one(B) is opened when i click button link in the Page(A).

One(A) have a timer as following code. Use GetAttribute to check bsModal or bsAlert from other page(websocket). I will send event to client for showing a "modal dialog" when msg<>"".

B4X:
private Sub timer1_tick
    timer1.Enabled=False
    Dim msg As String=ws.Session.GetAttribute("bsModal")
    If msg <> "" Then
        ws.RunFunction("messagebox",Array As Object(msg))
        ws.Session.SetAttribute("bsModal","")
        ws.Flush
    End If
    msg=ws.Session.GetAttribute("bsAlert")
    If msg <> "" Then
        ws.GetElementById("bsalert").SetHtml(msg)
        ws.Session.SetAttribute("bsAlert","")
        ws.Flush
    End If
    timer1.Enabled=True
End Sub

Page(B) code as following: save data and notify page(A) to showing "modal dialog"
B4X:
Sub btnWrite_Click (Params As Map)
    Dim ft8 As Future = plc1.GetProp("checked")
    Dim ft9 As Future = interval1.GetVal
    Dim ft10 As Future = ip1.GetVal
    Dim ft11 As Future = port1.GetVal
    Dim ft12 As Future = address1.GetVal
    Dim ft13 As Future = size1.GetVal
    If ft8.Value=True Then
        If ft9.Value<1 Then
            WebUtils.bsAlert(ws,"請設定PLC讀取時間>0")
            Return
        End If
        '
        If ft10.Value="" Then ' 192.168.1.1
            Dim ss() As String = Regex.Split(".",ft10.Value)
            If ss.Length<>4 Then
                WebUtils.bsAlert(ws,"PLC位址有誤")
                Return
            End If
            If Not(IsNumber(ss(0)) And IsNumber(ss(1)) And IsNumber(ss(2)) And IsNumber(ss(3))) Then
                WebUtils.bsAlert(ws,"PLC位址有誤")
                Return
            End If   
        End If
        '
        If Not(IsNumber(ft11.Value)) Then
            WebUtils.bsAlert(ws,"PLC埠號有誤")
            Return
        End If   
    End If
    Devices.plc.used = ft8.Value
    Devices.plc.interval = ft9.Value
    Devices.plc.ip = ft10.Value
    Devices.plc.port = ft11.Value
    Devices.plc.address = ft12.Value
    Devices.plc.size = ft13.Value
    Dim ft As Future=word1.GetProp("checked")
    Devices.plc.word1 = ft.Value
    For n1=0 To Devices.Size-1
        ft8=ws.GetElementById("inp"&(n1+1)).Getval
        Devices.Title(n1)=ft8.Value
        ft8=ws.GetElementById("used"&n1).GetProp("checked")
        Devices.used(n1)=ft8.Value
    Next
    WebUtils.bsMessageBox(ws,"PLC參數存檔完成")                           <<<<<----- Send "bsModal" to Page(A)
    Devices.save       
    Log("param/plc save")
End Sub

B4X:
Sub bsMessageBox(ws As WebSocket,msg As String)
    ws.Session.SetAttribute("bsModal",msg)
End Sub

page(A) client side of HTML
B4X:
<script language="JavaScript">
        function navi_click(n) {
            b4j_raiseEvent("navi_click", {"name": n});
        }
        function messagebox(msg) {
            document.getElementById("myMessage").innerHTML = msg
            $('#myModal').modal({show:true});
        }
    </script>
</head>

<body>
    <!-- Modal-dialog -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
                    <h4 class="modal-title">訊 息</h4>
                </div>
                <div id="myMessage" class="modal-body">
                    Content for the dialog / modal goes here.
                </div>                   
            </div>
        </div>
    </div>
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add log messages in WebSocket_Disconnected events (in both pages) and see whether this error happens after one of the websockets has disconnected.

What is the timer interval?

Note that there are better ways to communicate between the two pages. Page A can add itself to the session in Websocket_Connect:
B4X:
ws.Sesson.SetAttribute("PageA", Me)

Remove it in WebSocket_Disconnect.

PageB can then use CallSubDelayed to communicate with PageA:
B4X:
If ws.Session.HasAttribute("PageA") Then 
 Dim a As PageA = ws.Session.Get("PageA")
 CallSubDelayed2(a, "SomeSub", ...)
End If
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

Sorry later! My PLC library is for communicating with mitsubishi-PLC with their MC protocol througth Ethernet.
 
Upvote 0
Top