This shows you the differences between two versions of the page.
built_in:webserver [2015/07/07 09:31] octazid [Example] |
built_in:webserver [2016/07/16 01:37] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== WebServer control ====== | ||
- | ===== Methods ===== | ||
- | ^ Method | ||
- | | WebServer.AddServlet( p1,callback ) | | | ||
- | | WebServer.SendText( txt ) | | | ||
- | | WebServer.SetFolder( folder ) | | | ||
- | | WebServer.SetResponse( text ) | | | ||
- | | WebServer.Start() | ||
- | |||
- | =====Example===== | ||
- | //(Example taken from the DroidScript sample section)// | ||
- | |||
- | <code javascript> | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | var ip = app.GetIPAddress(); | ||
- | if( ip == " | ||
- | app.ShowPopup( " | ||
- | app.Exit(); | ||
- | } | ||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | // | ||
- | var s = " | ||
- | " | ||
- | txt = app.CreateText( s, | ||
- | txt.SetTextSize( 22 ); | ||
- | lay.AddChild( txt ); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | |||
- | // | ||
- | serv = app.CreateWebServer( 8080, | ||
- | serv.SetFolder( "/ | ||
- | serv.AddServlet( "/ | ||
- | serv.Start(); | ||
- | } | ||
- | |||
- | // | ||
- | function OnServlet( request, | ||
- | { | ||
- | serv.SetResponse( " | ||
- | app.ShowPopup( info.remoteAddress + " | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | |||
- | =====WebSocket Sample===== | ||
- | running on DroidScript V 1.23b and higher | ||
- | |||
- | //(Sample posted by Dave Smart in the DroidScript Beta Forum)// | ||
- | |||
- | |||
- | Create the following two files in one folder and call your folder "Web Sockets" | ||
- | |||
- | <code html index.html> | ||
- | < | ||
- | < | ||
- | < | ||
- | |||
- | < | ||
- | |||
- | var count = 0; | ||
- | |||
- | //Connect to server via web sockets. | ||
- | function Connect() | ||
- | { | ||
- | //Check web sockets are supported. | ||
- | if (!window.WebSocket) | ||
- | { | ||
- | alert(" | ||
- | return; | ||
- | } | ||
- | |||
- | //Open web socket to phone. | ||
- | ws = new WebSocket( " | ||
- | ws.onopen = ws_onopen; | ||
- | ws.onmessage = ws_onmessage; | ||
- | ws.onclose = ws_onclose; | ||
- | ws.onerror = ws_onerror; | ||
- | } | ||
- | |||
- | //Send a message to the server. | ||
- | function Send() | ||
- | { | ||
- | ws.send( "Hello " + count++ ); | ||
- | } | ||
- | |||
- | //Handle socket open. | ||
- | function ws_onopen() | ||
- | { | ||
- | id_info.innerHTML = " | ||
- | } | ||
- | |||
- | //Handle messages from phone. | ||
- | function ws_onmessage( msg ) | ||
- | { | ||
- | id_info.innerHTML = msg.data; | ||
- | } | ||
- | |||
- | //Other websocket callbacks. | ||
- | function ws_onclose() { id_info.innerHTML = " | ||
- | function ws_onerror(e) { id_info.innerHTML = " | ||
- | </ | ||
- | </ | ||
- | |||
- | < | ||
- | < | ||
- | |||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | <code javascript Web Sockets.js> | ||
- | //Init variables. | ||
- | var count = 0; | ||
- | |||
- | //Called when application is started. | ||
- | function OnStart() | ||
- | { | ||
- | //Check wifi is enabled. | ||
- | ip = app.GetIPAddress(); | ||
- | if( ip == " | ||
- | app.ShowPopup( " | ||
- | app.Exit(); | ||
- | } | ||
- | |||
- | //Prevent wifi from powering down. | ||
- | app.PreventWifiSleep(); | ||
- | |||
- | //Create a layout with objects vertically centered. | ||
- | lay = app.CreateLayout( " | ||
- | |||
- | //Create a text label and add it to layout. | ||
- | var s = "Type the following address into your" + | ||
- | " browser(s)\n\n" | ||
- | txt = app.CreateText( s, 0.8, 0.3, " | ||
- | txt.SetTextSize( 22 ); | ||
- | lay.AddChild( txt ); | ||
- | |||
- | //Create a text label and add it to layout. | ||
- | txtMsg = app.CreateText( "", | ||
- | txtMsg.SetTextSize( 22 ); | ||
- | lay.AddChild( txtMsg ); | ||
- | |||
- | //Create a 'Send Message' | ||
- | btn = app.CreateButton( "Send Message", | ||
- | btn.SetMargins( 0, 0.05, 0, 0 ); | ||
- | btn.SetOnTouch( SendMessage ); | ||
- | lay.AddChild( btn ); | ||
- | |||
- | //Add layout to app. | ||
- | app.AddLayout( lay ); | ||
- | |||
- | //Create and run web server on port 8080. | ||
- | serv = app.CreateWebServer( 8080 ); | ||
- | serv.SetFolder( "/ | ||
- | serv.SetOnReceive( serv_OnReceive ); | ||
- | serv.Start(); | ||
- | |||
- | //Start timer to show WebSock connections. | ||
- | setInterval( ShowConnections, | ||
- | } | ||
- | |||
- | //Show who is connected. | ||
- | function ShowConnections() | ||
- | { | ||
- | var clients = serv.GetWebSockClients(); | ||
- | |||
- | if( clients.length > 0 ) | ||
- | { | ||
- | //Make a list of clients. | ||
- | var list = ""; | ||
- | for( var i=0; i< | ||
- | list += clients[i].remoteAddress + " | ||
- | | ||
- | //Show client list. | ||
- | txt.SetText( list ); | ||
- | } | ||
- | } | ||
- | |||
- | //Send a message to all connected socket clients. | ||
- | function SendMessage() | ||
- | { | ||
- | //Note: You can send to a specific client by passing | ||
- | //the IP address as the second parameter. | ||
- | serv.SendText( "Hello " + count++ ) | ||
- | } | ||
- | |||
- | //Called when messages arrive from websocket clients. | ||
- | function serv_OnReceive( msg, ip ) | ||
- | { | ||
- | txtMsg.SetText( ip + ": " + msg ); | ||
- | } | ||
- | </ |