2015年11月29日 星期日

jSocket code

<div id='mySocket'>
<button type="button" onclick="openTCP()">TCP Open</button>
<button type="button" onclick="closeTCP()">TCP Close</button>
</div>

<script src="https://jsocket.googlecode.com/svn-history/r119/trunk/jsocket/src/jsocket.js"></script>



<script>
var socket = null;

function openTCP() {
    socket = new jSocket();  
    socket.onReady = function(){
       socket.connect('192.168.27.245', 7142);          
    }
 
    // Connection attempt finished
        socket.onConnect = function(success, msg){
                if(success){
                        // Send something to the socket
                        console.log("TCP connection established sucessfully");
                        socket.write('Hello world');          
                }else{
                        console.log("TCP connection established fail");
                        alert('Connection to the server could not be estabilished: ' + msg);          
                }    
        }

        socket.onData = function(data){
                alert('Received from socket: '+data);
        }
     
        // Setup our socket in the div with the id="socket"
        socket.setup('mySocket');  
}

function closeTCP() {
    socket.halfClose();
    socket.close();
    if (socket.closed) {
      console.log("TCP socket has been cleanly closed");
    };
}

</script>

沒有留言:

張貼留言