B4J Question WebApp Chatroom - A server error

LucaMs

Expert
Licensed User
Longtime User
I'm trying the example WebApp Chatroom.


ChatShared routine:
B4X:
Public Sub NewMessage(Name As String, msg As String)
    LastMessages.Add(msg)
    If LastMessages.Size > 10 Then
        LastMessages.RemoveAt(0)
    End If
    'notify each of the connected users about the new message
    For Each c As Chat In connections.Values
        CallSubDelayed2(c, "NewMessage", msg)
    Next
End Sub

It does not use the parameter Name, but I thought to use it this way,
changing the call:
B4X:
CallSubDelayed3(c, "NewMessage", msg, Name)


Chat routine:
B4X:
Sub NewMessage(Msg As String, Writer As String)
    ChatTxt.RunMethod("append", Array As Object(Msg))
    ws.RunFunction("scrollDown", Null)
    ws.Flush

    If Writer <> name Then ' name is a module variable that holds the name of the chatter
        ' do something because you are not the sender
    End If
End Sub

but I get this server error:
java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: 1
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:456)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:427)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:501)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:195)

...
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at b4j.example.chat.callSub(chat.java:146)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:431)
... 10 more



[P.S. the aim is to detect that the message received was sent by "themselves".
(I could get this from the message, I know, but I do not understand, the error seems to be the use of CallSubDelayed3 instead of CallSubDelayed2)]
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
There are few unused files in www.

The project is your project, I changed only one line in ChatShared:
B4X:
CallSubDelayed3(c, "NewMessage", msg, Name)
instead of
B4X:
CallSubDelayed2(c, "NewMessage", msg)



and the called routine, in Chat:
B4X:
Sub NewMessage(Msg AsString, Writer As String)
instead of
B4X:
Sub NewMessage(Msg AsString)

(also, I added 4 lines here:
B4X:
If Writer <> name Then
    ws.RunFunction("PlaySound", Null)
    ws.Flush
End If


Thank you
 

Attachments

  • Chatroom.zip
    401.1 KB · Views: 211
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Search for "new

The result is:
SS-2015-10-02_13.35.30.png


The first result is a call that you haven't modified.
 
Upvote 0
Top