//==========websocket(function($) { $.websocket = function(options) { var defaults = { domain: top.location.hostname, port:3398, protocol:"" }; var opts = $.extend(defaults,options); var szServer = "ws://" + opts.domain + ":" + opts.port + "/" + opts.protocol; var socket = null; var bOpen = false; var t1 = 0; var t2 = 0; var messageevent = { onInit:function(){ if(!("WebSocket" in window) && !("MozWebSocket" in window)){ return false; } if(("MozWebSocket" in window)){ socket = new MozWebSocket(szServer); }else{ socket = new WebSocket(szServer); } if(opts.onInit){ opts.onInit(); } }, onOpen:function(event){ bOpen = true; if(opts.onOpen){ opts.onOpen(event); } }, onSend:function(msg){ t1 = new Date().getTime(); if(opts.onSend){ opts.onSend(msg); } socket.send(msg); }, onMessage:function(msg){ t2 = new Date().getTime(); if(opts.onMessage){ opts.onMessage(msg.data,t2 - t1); } }, onError:function(event){ if(opts.onError){ opts.onError(event); } }, onClose:function(event){ if(opts.onclose){ opts.onclose(event); } if(socket.close() != null){ socket = null; } } } messageevent.onInit(); socket.onopen = messageevent.onOpen; socket.onmessage = messageevent.onMessage; socket.onerror = messageevent.onError; socket.onclose = messageevent.onClose; this.send = function(pData){ if(bOpen == false){ return false; } messageevent.onSend(pData); return true; } this.close = function(){ messageevent.onClose(); } return this; };})(jQuery);
I wrote the back-end management part of my website again. I used Ajax in the past. I did a very powerful job in the collection part and made it a webpage version. Python serves as the server, I need to be able to communicate with the browser in real time so that I can know the collection Progress. before using the PHP curl pipeline for batch collection, Ajax returns the progress, not very real-time, and too dead, after a long time, I switched to websocket to obtain the server details in real time.
This is why I encapsulated websocket into the jquery plug-in to facilitate calling and make the code more concise. My background management is a Web desktop form that enables each function application, it is mainly collection. You must use websocket to enable a link to communicate with the server of Python. The original method will make the structure very unclear, so encapsulate it and use it. See the following example.
This is how to use the websocket plug-in.
// ========== Start a websocket var socket1 =1. websocket ({domain: "www.qhnovel.com", // This is the domain name with the server or IP port: 8080, // This is the server port number Protocol: "text", // This is dispensable, the combination is WS: // www.qhnovel.com: 8080/test onopen: function (event) {alert ("handshaking with the server, onopen can be omitted without writing");}, onerror: function (event) {alert ("an error occurred, onerror can be omitted without writing") ;}, onsend: function (MSG) {alert ("additional code for sending data, can be omitted without writing ") ;}, onmessage: function (result, ntime) {alert (" data received from the server: "+ result ); alert ("Total time used to receive the last data sent to the present:" + ntime );}}); // =========== send data in socket1.send ("data to be sent "); // ========== close socket1.close ();
Socket. bopen = false when not connected to the server
The websocket client itself is relatively simple. Here the structure is clearer when a large number of requests are used. When I communicate with the python server, the data is base64-encoded and sent in JSON format, chinese garbled characters appear in base64 decoding by JS. See my other article.
For more information about how to implement the new version of websocket server in Python, see my other article.
The code is complete and clear. You can copy and paste the Code directly.
From: http://0x14.iteye.com