DroidScript + NodeJS Chat Example

1,014 views
Skip to first unread message

Teksys

unread,
Oct 12, 2016, 4:36:26 PM10/12/16
to DroidScript
Using NodeJS  https://nodejs.org . download install to ur pc or server .

On DroidScript  Create HTML APP

<html>
    <head>
        <script src="file:///android_asset/app.js"></script>
        <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
        <script>
            function OnStart() {
                app.ShowPopup( "NODEJS + DROIDScript Rocks!" );
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var socket = io('http://192.168.1.100:3000');
       
            document.documentElement.style.position = "absolute";
            document.documentElement.style.top = 0 + "px";
            document.documentElement.style.left = 0 + "px";
            document.documentElement.style.right = 0 + "px";
            document.documentElement.style.bottom = 0 + "px";
            document.documentElement.style.margin = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            document.documentElement.style.padding = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            document.documentElement.style.border = 0 + "px solid transparent";
   
            document.body.style.position = "absolute"; 
            document.body.style.top = 0 + "px";
            document.body.style.left = 0 + "px";
            document.body.style.right = 0 + "px";
            document.body.style.bottom = 0 + "px";
            document.body.style.margin = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            document.body.style.padding = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            document.body.style.border = 0 + "px solid transparent";
            document.body.style.backgroundColor="grey";
            document.body.onload = function() { app.Start(); };
            var ul = document.createElement('ul');
            ul.style.position = "absolute";
            ul.style.top = 0 + "px";
            ul.style.left = 0 + "px";
            ul.style.right = 0 + "px";
            ul.style.bottom = 40 + "px";
            ul.style.margin = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            ul.style.padding = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            ul.style.borderBottom = 1 + "px solid black";
            ul.style.backgroundColor="white";
            ul.style.listStyleType = "none";
            ul.style.overflowY = "scroll";
            ul.id = "messages";
            document.body.appendChild(ul);
            var input = document.createElement('input');
            input.setAttribute("type", "text");
            input.style.position = "absolute";
            input.style.height = 40 + "px";
            input.style.left = 0 + "px";
            input.style.width = document.body.clientWidth - 60 + "px";
            input.style.bottom = 0 + "px";
            input.style.margin = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            input.style.padding = 0 + "px " + 0 + "px " + 0 + "px " + 0 + "px";
            input.style.border = 0 + "px solid transparent";
           
            input.addEventListener('keypress', function (e) {
                var key = e.which || e.keyCode;
                if (key === 13) {
                    socket.emit('message', input.value);
                    input.value = '';
               }
           });
           document.body.appendChild(input);
            var button = document.createElement('button');
            button.style.position = "absolute";
            button.style.height = 40 + "px";
            button.style.right = 0 + "px";
            button.style.width = 60 + "px";
            button.style.bottom = 0 + "px";
            button.innerText = "SEND";
            button.addEventListener('click', function() {
                socket.emit('message', input.value);
                input.value = '';
            });
            document.body.appendChild(button);
            var num = 0;
            var li = [];
            document.head.innerHTML += "<style> #messages li:nth-child(odd) { background: #eee; } </style>";
   
            socket.on('message', function(data){
                li[num] = document.createElement('li');
                li[num].style.padding = 5 + "px " + 10 + "px " + 0 + "px " + 0 + "px";
                li[num].innerText = data;
                ul.appendChild(li[num]);
                num++;
            });
            window.addEventListener('resize', function() {
                input.style.width = document.body.clientWidth - 60 + "px";
            }, false);
        </script>
    </body>
</html>




CREATE FOLDER Name "NodeServer"

SAVE THE SCRIPT BELOW  INTO THE FOLDER AS server.js

var io = require('socket.io')(3000);
console.log('listening on *:3000');
io.on('connection', function(socket){
 console.log("User Online ("+socket.request.connection.remoteAddress+")\n");
 socket.on('message', function(data){
  if (data == '') { return false; }
  io.emit('message', data);
 });
 
 socket.on('disconnect', function(socket){
  console.log("User OffLine ("+socket.request.connection.remoteAddress+")\n");
 });
});

open node console, cd into NodeServer
npm init
npm install socket.io --save

nodejs server.js  TO START SERVER


make sure u using npm command "node package manager"  



Symbroson Development

unread,
Oct 12, 2016, 5:02:15 PM10/12/16
to DroidScript
Can you make an spk please - makes it easyer to try it out ^^

On Devices just tap 'Desktop' at the bottom of the community
Thanks

Teksys

unread,
Oct 12, 2016, 8:31:44 PM10/12/16
to DroidScript
hmmm spk? I'm new to droidscript..  

Symbroson Development

unread,
Oct 13, 2016, 12:09:58 AM10/13/16
to DroidScript
Use the 'Share via email' button when longoress an app icon in DS.
Then an spk were sent

Questao De 5 Minutos

unread,
Apr 24, 2020, 9:31:13 AM4/24/20
to DroidScript
resurrecting this post, because I have an application in nodejs running locally on my machine, and I would like to run on droidscript, someone did, because in the code above the friend there, the second part is not clear on what to do

John

unread,
May 13, 2020, 9:13:56 PM5/13/20
to DroidScript
@Questao De 5 Minutos: okay, so you have a node app you want to run on your android device. The instructions above aren't for that at all. If you don't need to access android hardware features (such as camera) why not use a Node-on-device app like Termux to run it? Otherwise, look into packaging your node app with something like RollupJS to create a file that DroidScript can handle.
Reply all
Reply to author
Forward
0 new messages