B4J Question SendEventToServer isn't working !

jinyistudio

Well-Known Member
Licensed User
Longtime User
I can runfunction to client.
when I use SendEventToServer in the client(b4j app) Why server side always give me Runtime Exception:Invilid event name : fromclient1 ? :mad:

jserver V3.0
jwebsocketclient V1.10

client side code
B4X:
Sub wsc_connected
    Log($"gate connected. ${Main.gateaddr}"$)
    SendEventToServer("fromclient1",CreateMap("test":1))
End Sub

Public Sub SendEventToServer(Event As String, Data As Map)
    Dim m As Map
    m.Initialize
    m.Put("type", "event")
    m.Put("event", Event)
    m.Put("params", Data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    wsc.SendText(jg.ToString)
End Sub

Private Sub wsc_TextMessage(msg As String)
    Try
        Dim jp As JSONParser
        jp.Initialize(msg)
        Dim m As Map = jp.NextObject
        Dim etype As String = m.get("etype")
        Dim params As List = m.get("value")
        Dim event As String = m.get("prop")
      
        If etype = "runFunction" Then
            CallSub2(Me, "wsc_"  & event, params)
          
        Else If etype = "runFunctionWithResult" Then
            Dim data As Map = CallSub2(Me, "wsc_" & event, params)
            Dim jg As JSONGenerator
            Dim m2 As Map : m2.Initialize
            m2.Put("type", "data")
            m2.Put("data", data)
            jg.Initialize(m2)
            wsc.SendText(jg.ToString)
          
        End If
    Catch
        Log("TextMessage Error: " & LastException)
    End Try
End Sub
server side code
B4X:
'WebSocket class
Sub Class_Globals
    Private     ws As WebSocket
    Private     item As Map
    Private     timer1 As Timer
End Sub

Public Sub Initialize
    timer1.Initialize("timer1",500)
    timer1.Enabled=False
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1 
    Log($"Device connected. ${ws.UpgradeRequest.RemoteAddress}"$) 
End Sub

Private Sub WebSocket_Disconnected
    Log($"Device disconnected."$)
End Sub

Sub fromclient1(Params As Map)
    Log("Device message: ")
End Sub
 
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
I forget that my servser is abmaterial v4.30
B4X:
2019-10-23 13:49:35.005:INFO:oejs.session:main: node0 Scavenging every 600000ms
2019-10-23 13:49:35.007:INFO:oejs.AbstractConnector:main: Started ServerConnecto
r@eeefeb{HTTP/1.1,[http/1.1]}{0.0.0.0:8110}
2019-10-23 13:49:35.009:INFO:oejs.Server:main: Started @1661ms
2019-10-23 13:49:35.010:INFO:oejs.session:main: node0 Scavenging every 990000ms
sub.Connected
pub.Connected
Device connected. 192.168.1.106
RunFunction gate
java.lang.RuntimeException: Invalid event name: fromclient1
        at anywheresoftware.b4j.object.WebSocketModule$Adapter.onWebSocketText(W
ebSocketModule.java:115)
        at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.on
TextMessage(JettyListenerEventDriver.java:189)
        at org.eclipse.jetty.websocket.common.message.SimpleTextMessage.messageC
omplete(SimpleTextMessage.java:69)
        at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.appendM
essage(AbstractEventDriver.java:66)
        at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.on
TextFrame(JettyListenerEventDriver.java:158)
        at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.incomin
gFrame(AbstractEventDriver.java:162)
        at org.eclipse.jetty.websocket.common.WebSocketSession.incomingFrame(Web
SocketSession.java:476)
        at org.eclipse.jetty.websocket.common.extensions.ExtensionStack.incoming
Frame(ExtensionStack.java:220)
        at org.eclipse.jetty.websocket.common.Parser.notifyFrame(Parser.java:220
)
        at org.eclipse.jetty.websocket.common.Parser.parse(Parser.java:245)
        at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.rea
dParse(AbstractWebSocketConnection.java:560)
        at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onF
illable(AbstractWebSocketConnection.java:391)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(Abstra
ctConnection.java:281)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhat
YouKill.java:333)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWh
atYouKill.java:310)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatW
hatYouKill.java:168)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhat
YouKill.java:132)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPoo
l.java:760)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool
.java:678)
        at java.lang.Thread.run(Unknown Source)
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi,It is OK. change fromclient1 to from_client o_O

client side
B4X:
wsc.SendEventToServer("from_client",CreateMap("test":1))
server side
B4X:
Sub from_client(Params As Map)
    Log("Device message: ")
End Sub
 
Upvote 0
Top