最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript websocket onmessage event.data - Stack Overflow

programmeradmin1浏览0评论

Using JavaScript WebSocket how to pass event.data out onMessage function?

var eventData = EventRequest("text");


  ..... codes .....


EventRequest = function (text)
{
   var socket = new WebSocket ('ws://localhost:8080/');
   websocket.onopen = function(evt) { onOpen(evt); };
   websocket.onmessage = function(evt) { onMessage(evt); };

function onOpen (evt)
{
   socket.send("text");
}

function onMessage (evt)
{
   alert (evt.data);
   return evt.data;
}
};

I tried different ways to pass evt.data out, but I have not been able to. I can see the correct evt.data data. I just can not pass the data out of onMessage function.

I tried

function wcConnection (){
   this.dataInput = '';
}

Inside onMessage function, I added

function onMessage (evt)
{
   alert (evt.data);
   this.dataInput = evt.data;
}

Any help would be appreciated.

Using JavaScript WebSocket how to pass event.data out onMessage function?

var eventData = EventRequest("text");


  ..... codes .....


EventRequest = function (text)
{
   var socket = new WebSocket ('ws://localhost:8080/');
   websocket.onopen = function(evt) { onOpen(evt); };
   websocket.onmessage = function(evt) { onMessage(evt); };

function onOpen (evt)
{
   socket.send("text");
}

function onMessage (evt)
{
   alert (evt.data);
   return evt.data;
}
};

I tried different ways to pass evt.data out, but I have not been able to. I can see the correct evt.data data. I just can not pass the data out of onMessage function.

I tried

function wcConnection (){
   this.dataInput = '';
}

Inside onMessage function, I added

function onMessage (evt)
{
   alert (evt.data);
   this.dataInput = evt.data;
}

Any help would be appreciated.

Share Improve this question edited Mar 28, 2013 at 4:19 Cris 13.4k5 gold badges36 silver badges51 bronze badges asked Mar 20, 2013 at 2:23 user33054user33054 911 gold badge1 silver badge3 bronze badges 1
  • can you do a basic one? tutorialspoint.com/html5/html5_websocket.htm like on that page? – user1382306 Commented Jul 6, 2013 at 14:31
Add a comment  | 

3 Answers 3

Reset to default 8

If you server is python tornado

def on_message(self, message):
        t = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
        self.write_message(t)

In your client, to retrieve the message, you could do

ws.onmessage = function (evt) { 
    console.log(JSON.parse(event.data));
}

you should see the json in the console

Why are you returning value to websocket.onmessage function? This is the function where you got that value. If you want to pass the value to another function just pass the event object to that function and access it using "evt.data".

websocket.onmessage = function(evt) { responseData(evt); };
function responseData(evt) {
    /* Here is your data, Do you what you want! */
    console.log(JSON.parse(evt.data));
}

Once you created an instance of your websocket using new , will be able to get message event OnMessage . If you using react then ,

    useEffect(() => {
    WebSocketScale.onmessage = (event: any) => {
        value.push(JSON.parse(event.data));
}, []);
发布评论

评论列表(0)

  1. 暂无评论