Run socket Server in a ionic1 application

Question

Start a socket server in the app which will receive messages from another app running in the same device.

Here is the stack:

Using the chrome’s tcpServer plugin, I ended this code:


angular</span><span class="pun">.</span><span class="kwd">module</span><span class="pun">(</span><span class="str">'starter'</span><span class="pun">,</span> <span class="pun">[</span><span class="str">'ionic'</span><span class="pun">])</span>
<span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">$ionicPlatform</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
    $ionicPlatform</span><span class="pun">.</span><span class="pln">ready</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span>
        <span class="kwd">var</span><span class="pln"> tcpServer </span><span class="pun">=</span><span class="pln"> window</span><span class="pun">.</span><span class="pln">chrome </span><span class="pun">&&</span><span class="pln"> window</span><span class="pun">.</span><span class="pln">chrome</span><span class="pun">.</span><span class="pln">sockets </span><span class="pun">&&</span><span class="pln">
            window</span><span class="pun">.</span><span class="pln">chrome</span><span class="pun">.</span><span class="pln">sockets</span><span class="pun">.</span><span class="pln">tcpServer</span><span class="pun">;</span>

        <span class="kwd">if</span><span class="pun">(</span><span class="pln">tcpServer</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">  
            console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'LOG tcpServer present'</span><span class="pun">)</span><span class="pln">
            tcpServer</span><span class="pun">.</span><span class="pln">create</span><span class="pun">({},</span> <span class="kwd">function</span> <span class="pun">(</span><span class="pln">createInfo</span><span class="pun">)</span> <span class="pun">{</span>
                <span class="kwd">var</span><span class="pln"> serverSocketId </span><span class="pun">=</span><span class="pln"> createInfo</span><span class="pun">.</span><span class="pln">socketId</span><span class="pun">;</span><span class="pln">
                console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'LOG'</span><span class="pun">,</span> <span class="str">'serverSocketId'</span><span class="pun">,</span><span class="pln"> serverSocketId</span><span class="pun">)</span>
                <span class="kwd">if</span> <span class="pun">(</span><span class="pln">serverSocketId </span><span class="pun">></span> <span class="lit">0</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
                    tcpServer</span><span class="pun">.</span><span class="pln">listen</span><span class="pun">(</span><span class="pln">serverSocketId</span><span class="pun">,</span> <span class="str">'0.0.0.0'</span><span class="pun">,</span> <span class="lit">8080</span><span class="pun">,</span> <span class="lit">50</span><span class="pun">,</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">resultCode</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
                        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'LOG'</span><span class="pun">,</span> <span class="str">'listening'</span><span class="pun">,</span><span class="pln"> resultCode</span><span class="pun">)</span>
                    <span class="pun">});</span>
                <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span><span class="pln">
                  console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'LOG'</span><span class="pun">,</span> <span class="str">'Unable to create socket'</span><span class="pun">);</span>
                <span class="pun">}</span>

            <span class="pun">});</span>
        <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span><span class="pln">
            console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'LOG'</span><span class="pun">,</span> <span class="str">'missing chrome.sockets.tcpServer'</span><span class="pun">)</span>
        <span class="pun">}</span>
    <span class="pun">});</span>
<span class="pun">})</span></code></pre>
in progress 0
admin 5 months 1 Answer 191 views 0

Answer ( 1 )

  1. Run socket Server in a ionic1 application – CopyStatus

Leave an answer

Browse
Browse

Captcha Click on image to update the captcha .