V
V
Vitalychez2015-11-03 10:18:54
JavaScript
Vitalychez, 2015-11-03 10:18:54

How to receive data via websocket?

Hello! Tell me how to pull data from websocket?
The point is that it sends undefined . When creating an interval, the data comes, but it is unreasonable to get it by the interval, since the server response may be different.

function connectSocket(param, subject, method) {
    var socket = new WebSocket("ws://localhost");
    var id = randomId();
    var dataSocket;
    socket.onopen = function() {
        var query = '{"id": "'+ id +'", "type": "request", "version": "v.2.0", "method": "'+ method +'", "subject": "'+ subject +'", "parameters": {'+ param +'}}';
        socket.send(query);
    };

    socket.onclose = function(event) {
        var dataSocket = 'Код: ' + event.code + ' причина: ' + event.reason;
    };

    socket.onmessage = function(event) {
        outputSock(event.data);
    };

    socket.onerror = function(error) {
        var dataSocket = "Ошибка " + error.message;
    }

}

function outputSock(q) {
    window.Sockesss = q;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-11-03
@Vitalychez

but it is unreasonable to get it by interval

Well, don't get it, you have it
socket.onmessage = function(event) {
        outputSock(event.data);
    };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question