-1

I need to build a website in which Javascript is used as the client language to communicate with a python server over websockets. The website needs to be hosted on Linux. For now, I'm using command python -m SimpleHTTPServer to build a simple server, and the server is also on the Linux and using python.

How do I go about at setting up the javascript client websocket communication? Or how should I approach the architecture?

dorien
  • 5,265
  • 10
  • 57
  • 116
KevinLu
  • 47
  • 1
  • 3

1 Answers1

1

Assuming that your python websocket server is up an running. You can connect with it through your website through either html5:

socket= new WebSocket('ws://www.example.com:8000/somesocket');
socket.onopen= function() {
    socket.send('hello');
};
socket.onmessage= function(s) {
    alert('got reply '+s);
};

Or via

http://raw-sockets.sysapps.org/#interface-tcpsocket

https://www.w3.org/TR/tcp-udp-sockets/

navigator.tcpPermission.requestPermission({remoteAddress:"127.0.0.1", remotePort:6789}).then(
  () => {
    // Permission was granted
    // Create a new TCP client socket and connect to remote host
    var mySocket = new TCPSocket("127.0.0.1", 6789);

    // Send data to server
    mySocket.writeable.write("Hello World").then(
        () => {

            // Data sent sucessfully, wait for response
            console.log("Data has been sent to server");
            mySocket.readable.getReader().read().then(
                ({ value, done }) => {
                    if (!done) {
                        // Response received, log it:
                        console.log("Data received from server:" + value);
                    }

                    // Close the TCP connection
                    mySocket.close();
                }
            );
        },
        e => console.error("Sending error: ", e);
    );

Whatever your server looks like, you always need to make sure your server and client use the same "protocol", i.e. they use the same variables to send and order in which to send them.

If you also need to set up a socket server in python, I suggest you look at some tutorials online as this is a non-trivial task: http://www.binarytides.com/python-socket-server-code-example/

dorien
  • 5,265
  • 10
  • 57
  • 116
  • But I only use the command "python -m SimpleHTTPServer 8000" to host my website on Linux and open the website on Windows by 172.XX.XX.XX:8000. and It seems can't connect. – KevinLu Aug 22 '16 at 23:56
  • You should check to see if you don't have a firewall enabled. Does it work if you go to localhost:8080? – dorien Aug 22 '16 at 23:57
  • BTW,The website can display. – KevinLu Aug 22 '16 at 23:57
  • Please clarify. The website shows up but it "can't connect"? So what is the exact command that is giving an issue? – dorien Aug 22 '16 at 23:58
  • I was under the impressin you wanted to connect through websockets. If you just want to run python code on your computer and output it on a website. You may want to install a XAMPP (or WAMPP) server setup. Check the last answer here: http://stackoverflow.com/questions/7460938/how-to-run-python-script-in-webpage – dorien Aug 23 '16 at 00:01
  • OH! I mean the I can access the website by 127.XX.XXX.XXX:8000. The website can display mean the layout is correct but just the websocket can't work! Could you kindly give me some simple example about the client and server site code such as .html .js .py. thank you! – KevinLu Aug 23 '16 at 00:02
  • See my answer for exmple code for client + the link for example server code (that is a bit more long). – dorien Aug 23 '16 at 00:03
  • I see a lot of simple code but I can't try it successfully on Linux. Like: http://stackoverflow.com/questions/16608296/websocket-javascript-client-and-python-server-retreiving-garbage-in-output – KevinLu Aug 23 '16 at 00:10
  • Please tell me more details. Can you run the python server code in terminal. What happens when you run it. Which error do you get? – dorien Aug 23 '16 at 09:12