#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