Android Question WebSocket library error on connect [Official Library]

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
hi everyone , i am trying b4a with websocket server using libwebsocket on C++ , but show message : Server 404 not found on android application .

why it happens ? both are incompatible ?
 

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Sorry about that @Erel , below screenshots , and code ( i just change IP adress on Official Code )

this is application code :

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private wsh As WebSocketHandler
    Private link As String = "ws://192.168.1.4:8090"
End Sub

Sub Globals
    Private btnConnect As Button
    Private lblServerTime As Label
    Private lblStatus As Label
    Private btnSend As Button
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        wsh.Initialize(Me, "wsh")
    End If
    Activity.LoadLayout("1")
End Sub

Sub wsh_ServerTime(Params As List)
    'example of a server push message
    lblServerTime.Text = "Server Time: " & Params.Get(0)
End Sub

Sub btnSend_Click
    Dim data As Map
    data.Initialize
    data.Put("message", EditText1.Text)
    wsh.SendEventToServer("Device_Message", data)
    EditText1.RequestFocus
    EditText1.SelectAll
End Sub

Sub EditText1_EnterPressed
    If btnSend.Enabled Then btnSend_Click
End Sub

Sub wsh_Connected
    UpdateStatus
End Sub

Sub wsh_Closed (Reason As String)
    UpdateStatus
    ToastMessageShow(Reason, True)
End Sub

Sub Activity_Resume
     UpdateStatus
End Sub

Sub UpdateStatus
    If wsh.ws.Connected Then
        lblStatus.Text = "Status: Connected"
    Else
        lblStatus.Text = "Status: Disconnected"
    End If
    btnConnect.Enabled = Not(wsh.ws.Connected)
    btnSend.Enabled = wsh.ws.Connected
End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
End Sub

Sub btnConnect_Click
    wsh.Connect(link)
    lblStatus.Text = "Status: Connecting..."
End Sub

#Project Code download :
https://www.dropbox.com/s/g8csho6bwmosl0c/WebSocketExample.zip?dl=0

#Server download (Only a demo) :
https://www.dropbox.com/s/4npbb99jnxov2ha/ServerDemo.zip?dl=0

#Screenshot :
B4A Application Log = http://prntscr.com/ibp0j7
B4A Application Debug = http://prntscr.com/ibp31s
B4A Application Debug error when click on "Connect Button" or "btnConnect" = http://prntscr.com/ibp37m
JavaScript Websocket testing with this same server = http://prntscr.com/ibp283


JavaScript Code working with libwebsocket .
B4X:
<!DOCTYPE html>
  <meta charset="utf-8" />
  <title>WebSocket Test</title>
  <script language="javascript" type="text/javascript">

  var wsUri = "ws://192.168.1.4:8090";
  var output;

  function init()
  {
    output = document.getElementById("output");
    testWebSocket();
  }

  function testWebSocket()
  {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) { onOpen(evt) };
    websocket.onclose = function(evt) { onClose(evt) };
    websocket.onmessage = function(evt) { onMessage(evt) };
    websocket.onerror = function(evt) { onError(evt) };
  }

  function onOpen(evt)
  {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
  }

  function onClose(evt)
  {
    writeToScreen("DISCONNECTED");
  }

  function onMessage(evt)
  {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
  }

  function onError(evt)
  {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
  }

  function doSend(message)
  {
    writeToScreen("SENT: " + message);
    websocket.send(message);
  }

  function writeToScreen(message)
  {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
  }

  window.addEventListener("load", init, false);

  </script>

  <h2>WebSocket Test</h2>

  <div id="output"></div>
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
1) Phone may not be connected to same network (192.168.1.x)
2) Router that phone connects to may have wireless isolation set up. With this setup, the phone will not be able to connect to anything on the LAN (only WAN access allowed)

Just guessing, since you do not run both examples on the same device. You need to run the JavaScript example on the phone with the same network setup as the B4A app. If at that point the JavaScript app connects and the B4A does not, then you may have an issue. Until then, it's all up in the air.
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
But i am try connect on my vps ( firewall disabled and all ports opened ) , and i get same error on aplication " server 404.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Did you test the JavaScript version on your phone?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Yes (for the VPS, should not work for your 192.168.1.x connection).
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
i am try with 4G , Releasing APK , another Phone , another network , and all i get same error : " Server error 404 ( Not Found ) .
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
@Erel , i know , but if works localhost , works on vps too , i am showing what it works on localhost , but i dont understand why , dont connect only on " websocket" server , note : on socket server connect ...
 
Upvote 0
Top