B4J Question webapp : jetty how to use setMaxTextMessageSize

Status
Not open for further replies.

billzhan

Active Member
Licensed User
Longtime User
Yes, about 115kb(and maybe more). It's the data of a spreadsheet which can be edited in web page.

Another solution : the data can be sent by ajax to server and be saved, the ws event will be called after ajax success event.

I prefer to make it in the ws thread.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I actually think that it is better to use a servlet to upload the data.

However try this code:
B4X:
Sub SetMaxTextMessage(size As Int)
   Dim jo As JavaObject = ws
   jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
   jo.RunMethod("setMaxTextMessageSize", Array(size))
End Sub

Call SetMaxTextMessage(1024 * 1024) in WebSocket_Connected sub. The code will update the internal limit (note that I haven't tested whether it actually increased the limit or not).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I actually think that it is better to use a servlet to upload the data.

However try this code:
B4X:
Sub SetMaxTextMessage(size As Int)
   Dim jo As JavaObject = ws
   jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
   jo.RunMethod("setMaxTextMessageSize", Array(size))
End Sub

Call SetMaxTextMessage(1024 * 1024) in WebSocket_Connected sub. The code will update the internal limit (note that I haven't tested whether it actually increased the limit or not).

This code works; the change is accepted but anyway the server will accept max 128KB or it raises an exception and the websocket will be disconnected.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
However try this code:
B4X:
Sub SetMaxTextMessage(size As Int)
Dim jo As JavaObject = ws
jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
jo.RunMethod("setMaxTextMessageSize", Array(size))
End Sub
I tried this in an ABMaterial exercise I am playing with, my code is:
B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    'Log("Connected")       
    ws = WebSocket1   
    
    Private wssize As Int = 128000
    Dim jo As JavaObject = ws
    jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
    jo.RunMethod("setMaxTextMessageSize", Array(wssize))                  '<<<<<<<<<<<<<< line 40
    
    ABMPageId = ABM.GetPageID(Page, Name,ws)
    
    Main.Server.Connected(Me, ws, Page, ABMPageId)
    
    'Log(ABMPageId)
    Log("Connected: " & ABMPageId)
End Sub
But it bombs with:
Error occurred on line: 40 (CameraPage)
java.lang.RuntimeException: Method: setMaxTextMessageSize not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at com.ab.template.camerapage._websocket_connected(camerapage.java:389)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:204)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at anywheresoftware.b4j.object.JServlet.createInstance(JServlet.java:81)
at anywheresoftware.b4j.object.BackgroundWorkersManager$1.run(BackgroundWorkersManager.java:40)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at com.ab.template.main.main(main.java:29)
Any suggestions appreciated...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Grrrrr...

I just googled "java.lang.RuntimeException: Method: setMaxTextMessageSize not matched." and found this thread by Alain:

https://www.b4x.com/android/forum/threads/problems-with-jserver4-and-jetty11.141661/post-897938

the solution is:

B4X:
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    'Log("Connected")       
    ws = WebSocket1   
    
    Private wssize As Long = 128000
    Dim jo As JavaObject = ws
    jo = jo.GetFieldJO("session").RunMethod("getPolicy", Null)
    jo.RunMethod("setMaxTextMessageSize", Array(wssize))
    
    ABMPageId = ABM.GetPageID(Page, Name,ws)
    
    Main.Server.Connected(Me, ws, Page, ABMPageId)
    
    'Log(ABMPageId)
    Log("Connected: " & ABMPageId)
End Sub
And the magic sauce is:

Private wssize As Long = 128000

NOT

Private wssize As Int = 128000
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Status
Not open for further replies.
Top