B4J Question JWebSocketClient and jServer

alwaysbusy

Expert
Licensed User
Longtime User
I'm trying to connect a normal B4J program (using jWebSocketClient) with jServer (like the Push project), where the server is an ABMaterial app.

I use the default Push example, just setting my url to:
B4X:
Public serverLink As String = "ws://192.168.19.129:53001/BLEABMServer/PiPage"

In my StartServer, I set the AddWebSocket (among some others, needed for ABMaterial)
B4X:
public Sub StartServer(srvr As Server, srvrName As String, srvrPort As Int)    
   ABM.WriteAppLauchPageToDisk(AppPage, File.DirApp & "/www/" & ABMShared.AppName, "index.html", ABMShared.NeedsAuthorization)

   ' start the server
   srvr.Initialize(srvrName)

   srvr.AddWebSocket("/ws/" & ABMShared.AppName, "ABMApplication")
   For i =0 To Pages.Size - 1
     ' here ("/ws/BLEABMServer/PiPage", "PiPage") is added
     srvr.AddWebSocket("/ws/" & ABMShared.AppName & "/" & Pages.Get(i) , Pages.Get(i))
     If PageNeedsUpload.Get(i) Then      
       srvr.AddHandler("/" & ABMShared.AppName & "/" & Pages.Get(i) & "/abmuploadhandler", "ABMUploadHandler", False)
     End If
   Next  
   srvr.AddBackgroundWorker("ABMCacheScavenger")
   srvr.Port = srvrPort
   #If RELEASE
     srvr.SetStaticFilesOptions(CreateMap("cacheControl": "max-age=604800,public","gzip":True,"dirAllowed":False))
   #Else
     srvr.SetStaticFilesOptions(CreateMap("cacheControl": "max-age=604800,public","gzip":False,"dirAllowed":False))
   #End If
   srvr.Start    

   Dim joServer As JavaObject = srvr
  joServer.GetFieldJO("server").RunMethod("stop", Null)
  joServer.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("getSessionManager", Null).RunMethodJO("getSessionCookieConfig", Null).RunMethod("setMaxAge", Array(31536000)) ' 1 year
  joServer.GetFieldJO("server").RunMethod("start", Null)
    
   Dim jo As JavaObject = srvr
   Dim connectors() As Object = jo.GetFieldJO("server").RunMethod("getConnectors", Null)
   Dim timeout As Long = ABMShared.SessionMaxInactiveIntervalSeconds*1000
   For Each c As JavaObject In connectors
     c.RunMethod("setIdleTimeout", Array(timeout))
   Next

   ABMShared.CachedPages = srvr.CreateThreadSafeMap
End Sub

However, when I try to connect the normal push B4J app, I get the following error:
B4X:
org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols, expected status <101>, but got <302>
   at org.eclipse.jetty.websocket.client.io.UpgradeConnection.validateResponse(UpgradeConnection.java:352)
   at org.eclipse.jetty.websocket.client.io.UpgradeConnection.read(UpgradeConnection.java:280)
   at org.eclipse.jetty.websocket.client.io.UpgradeConnection.onFillable(UpgradeConnection.java:196)
   at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
   at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
   at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
   at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
   at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
   at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
   at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
   at java.lang.Thread.run(Thread.java:745)
WebSocket Closed: Didn't switch protocols, expected status <101>, but got <302>

Which sounds like a redirection error.

If I set the Url using http:// (like this):
B4X:
Public serverLink As String = "http://192.168.19.129:53001/BLEABMServer/PiPage"

Nothing happens (no error, no connection event). In a browser, the "http://192.168.19.129:53001/BLEABMServer/PiPage" works, connection events are raised.

Any pointers are welcome, as I am kind of stuck for the best part of last week on this and I just don't see it :(
 

alwaysbusy

Expert
Licensed User
Longtime User
You example server/client does indeed work just fine.

No idea why, but if I add the PiPage manually after the for loop, it looks like it is working
B4X:
... 
For i =0 To Pages.Size - 1
      ' NOT ADDING PiPage here
     srvr.AddWebSocket("/ws/" & ABMShared.AppName & "/" & Pages.Get(i) , Pages.Get(i))
     If PageNeedsUpload.Get(i) Then      
         srvr.AddHandler("/" & ABMShared.AppName & "/" & Pages.Get(i) & "/abmuploadhandler", "ABMUploadHandler", False)
     End If
Next  
srvr.AddWebSocket("/" & ABMShared.AppName & "/PiPage", "PiPage")  ' NOT IN THE LOOP
srvr.AddBackgroundWorker("ABMCacheScavenger")
srvr.Port = srvrPort
...

Note: I also did not generate a html file like I did for the other ABMaterial pages. (No folder, no html, no js, etc). Maybe Jetty sees a folder or .html file, and does something different. I'll do some more test later.

EDIT: this does not matter, as I just noticed the html, js and folder still exist from my previous trials.
 
Upvote 0

liulifeng77

Active Member
Licensed User
Longtime User
in the the main,I used:
Dim remo As remote
remo.Initialize
myApp.AddPage(remo.page)

remote page:
I didn't add any component.

It works well.(in b4j or b4a )
 
Upvote 0
Top