1: <?php
2:
3: /**
4: * Description of SocketOutput
5: * Socket work with native app to process as outsider from web server environment. This component
6: * create socket and
7: * display data send by native app.
8: * Front File code:-
9: * <div id="div1" runat="server" path="libpath/comp/ajax/SocketOutput.php"></div>
10: * Then call js function:-
11: * Params = $gate,$evt,$evtp,$data
12: * callNativeGate('shell','ls','-l',{});
13: * @author SARTAJ
14: */
15: class SocketOutput extends \Sphp\tools\Component {
16:
17: private $url = '';
18:
19: public function fu_setURL($param) {
20: $this->url = $param;
21: }
22:
23: protected function onjsrender() {
24: $protocol = "ws";
25: $this->setAttributeDefault('style', 'style="overflow-y: scroll; height: 500px; max-height: 500px;');
26: $this->setAttributeDefault('class', 'text-wrap');
27: addHeaderJSCode($this->name , 'window["'. $this->name .'"] = {wsobj: null, onopen: function(){}}; window["callNativeGate"] = function (ctrl,evt="",evtp="",data={}){
28: $("#'. $this->name .'").html(\'\');
29: window["'. $this->name .'"]["wsobj"].callProcessNativeGate(ctrl,evt,evtp,data);
30: };
31: ');
32: if (\SphpBase::sphp_request()->server('HTTPS') == 1) $protocol = "wss";
33: if ($this->url == '')
34: // $this->url = $protocol . '://' . SphpBase::sphp_request()->server('HTTP_HOST') . '/sphp.ws';
35: $this->url = SphpBase::sphp_request()->server('HTTP_HOST') ;
36: addHeaderJSFunctionCode("ready", "socketnative", 'frontobj.websockethost = "'. $this->url .'"; frontobj.getSphpSocket(function(wsobj1){
37: window["'. $this->name . '"]["wsobj"] = wsobj1;
38: window["'. $this->name . '"]["onopen"]();
39: frontobj.onwsmsg = function(msg){
40: $("#'. $this->name .'").append(\'<p>\' + msg + \'</p>\').scrollTop($("#'. $this->name .'").prop(\'scrollHeight\'));
41: };
42: });');
43: }
44:
45: }
46: