### Example HelloWorld

WebApp Example HelloWorld.
HTML
File Index.html in Objects\www folder
NOTE: Requires also b4j_ws.js
<!DOCTYPE html>
<html>
<head>
<title>WebApp Hello World</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<!-- Google Hosted Library for jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!-- B4J WebSockets client library -->
<script src="/b4j_ws.js"></script>
</head>
<body>
<H1>WebApp Hello World Example</H1>
<HR>
<div id="lblhelloworld"></div>
<HR>
<button id="btnabout" title="About">About</button>
<div class="author">by Robert W.B. Linn</div>
<!-- Script Area -->
<script>
$( document ).ready(function() {
//Use console logging for tests - example FireFox with FireBug
console.log('Document ready started.');
// Connect to the B4J Server = the handler helloworld.bas is called
b4j_connect("/ws");
});
</script>
</body>
</html>
B4J - helloworld.b4j
Version=2.9
AppType=StandardJava
IconFile=
NumberOfModules=1
Module1=helloworld
Build1=Default,b4j.example
NumberOfFiles=0
NumberOfLibraries=3
Library1=jcore
Library2=jserver
Library3=JavaObject
@EndOfDesignText@
'B4J WebApp to show how to display Hello World.
'Lookup www.index.html on how the html page is defined.
Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
End Region
`Sub Process_Globals
Public srvr As Server
End Sub`
`Sub AppStart (Args() As String)
Log("AppStart :: Start")
'
'Init the webserver
srvr.Initialize("srvr")
'Add the websockets, set the port and start the server
'Note: The port must be unique for this webapp
'The class helloworld is defined in helloworld.bas
srvr.AddWebSocket("/ws", "helloworld")
srvr.Port = 51042
srvr.Start
'
StartMessageLoop
'
Log("AppStart :: End")
End Sub`
B4J - helloworld.bas
Type=Class
Version=2.9
ModulesStructureVersion=1
B4J=true
@EndOfDesignText@
'
`Sub Class_Globals
Private ws As WebSocket
Private btnabout As JQueryElement 'Alert to show info about this app
Private lblhelloworld As JQueryElement 'Label showing Hello World
End Sub`
`Public Sub Initialize
End Sub`
' The user opens via browser :51042. The html file index.html is displayed.
`Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
Log("WebSocket_Connected :: Start")
ws = WebSocket1
lblhelloworld.setHtml("

### Hello World

" & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
Log("WebSocket_Connected :: End")
End Sub

`
'Not used
`Private Sub WebSocket_Disconnected
End Sub`
'Handle click on the button with id btnAddNewButton - Note in the index.html all id's are in lowercase
`Sub btnAbout_click(params As Map)
Log("btnabout_click :: Start = " & params.Size)
'In index.html a div with id btnabout is defined. This is used to display the about info.
ws.Alert("B4J HowTo Hello World" & CRLF & "by Robert W.B. Linn, Pinneberg, Germany")
Log("btnnabout_click :: End")
End Sub`
B4J - webutils.bas
Type=StaticCode
Version=2.8
ModulesStructureVersion=1
B4J=true
@EndOfDesignText@
`Sub Process_Globals
End Sub`
`Public Sub EscapeHtml(Raw As String) As String
Dim sb As StringBuilder
sb.Initialize
For i = 0 To Raw.Length - 1
Dim C As Char = Raw.CharAt(i)
Select C
Case QUOTE
sb.Append(""")
Case "'"
sb.Append("'")
Case "<"
sb.Append("<")
Case ">"
sb.Append(">")
Case "&"
sb.Append("&")
Case "Ä"
sb.Append("Ä")
Case "ä"
sb.Append("ä")
Case "Ü"
sb.Append("Ü")
Case "ü"
sb.Append("ü")
Case "Ö"
sb.Append("Ö")
Case "ö"
sb.Append("ö")
Case "ß"
sb.Append("ß")
Case Else
sb.Append(C)
End Select
Next
Return sb.ToString
End Sub`
`Public Sub ReplaceMap(Base As String, Replacements As Map) As String
For i = 0 To Replacements.Size - 1
Base = Base.Replace("$" & Replacements.GetKeyAt(i) & "$", Replacements.GetValueAt(i))
Next
Return Base
End Sub`
`Public Sub RedirectTo(ws As WebSocket, TargetUrl As String)
ws.Eval("window.location = arguments[0]", Array As Object(TargetUrl))
End Sub`

Changed: 2018-06-07 09:27, Category: WebApps, File: T6C277M2064.htm, ID: 2064