becvert/cordova-plugin-websocket-server

wsserver.send() not working

Gunasekaran15 opened this issue · 4 comments

I have applied wsserver.send(conn,msg) in my code but it not working.I request you to give a correct code to send message from server to client

Provide some debug log or sample of your code if you want us to help you.

$("#start").click(function(){
    var deviceMake=device.platform;
    var model=device.model;
    var serial=device.serial;
    var uuid=device.uuid;
    var k= "print response from web socket";
    alert("inside web");
    var timer = new Timer();
    timer.addEventListener('secondsUpdated', function (e) {
        $('#sampletimer .values').html(timer.getTimeValues().toString());
    }); 
    var wsserver = cordova.plugins.wsserver;
    var port=8081;
    remoteAddr="ws://127.0.0.1:8081";
        wsserver.start(port, {
            // WebSocket Server handlers
            'onFailure' :  function(addr, port, reason) {
                console.log('Stopped listening on %s:%d. Reason: %s', addr, port, reason);
            },
            // WebSocket Connection handlers
            'onOpen' : function(conn,msg) {
                console.log('A user connected from %s',remoteAddr);
            },
            'onMessage' : function(conn, msg) {
                //    console.log(conn, msg);
                if(conn.resource=='/print'){
                    console.log(conn);
                    console.log(msg);
                    console.log(conn.resource);
                    var a=msg;
                    window.DatecsPrinter.printText(a,'ISO-8859-1',success,error);
                    function success() {
                        //printMyImage();
                        //  alert('success!');
                        //  printQRCode();  
                    }
                    function error(){
                        alert(JSON.stringify("error is"+error));
                    }
                }
               this.send(k);

            },
            'onSend':function(conn,msg){
                wsserver.send({'uuid':'8e176b14-a1af-70a7-3e3d-8b341977a16e'}, msg);
            },
            'onClose' : function(conn, code, reason, wasClean) {
                console.log('A user disconnected from %s',remoteAddr);
            },
            // Other options
            //'origins' : [ 'file://' ], // validates the 'Origin' HTTP Header.
            //'protocols' : [ 'my-protocol-v1', 'my-protocol-v2' ], // validates the 'Sec-WebSocket-Protocol' HTTP Header.
            'tcpNoDelay' : true // disables Nagle's algorithm.
        }, 
        function onStart(addr, port) {
            console.log('Listening on %s:%d', addr, port);
        },
        function onDidNotStart(reason) {
            console.log('Did not start. Reason: %s', reason);
        });
});

onSend does not exist.
and use the correct connection's uuid!
wsserver.send({'uuid':'<correct uuid>'}, msg);
or keep a reference to your conn objects then
wsserver.send(conn, msg);

closing now