Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:41:02+00:00 2026-06-06T07:41:02+00:00

I was trying to make a Read\Write function for websocket but i’ve a problem…

  • 0

I was trying to make a Read\Write function for websocket but i’ve a problem…

var inarrivo = 0;
var risposta = "";

function RDW_Command(Stringa) {
    var Risposta = "";
    Stringa = "$" + Stringa;
    socket.send(Stringa);
    inarrivo = 0;
    while (inarrivo == 0) {
        doNothing();
    }
    return risposta;
}

function doNothing() {}
socket.onmessage = function (msg) {
    risposta = msg.data;
    inarrivo = 1;
};

The problem is that it freeze when it enter in the while loop….
Any idea to fix it? >.< thank you!!
Andrea

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:41:03+00:00Added an answer on June 6, 2026 at 7:41 am

    You program websockets in javascript in the usual (in javascript) event/callback based way.

    Here’s an example :

    var somePackage = {};
    somePackage.connect = function()  {
        var ws = new WebSocket('ws://'+document.location.host+'/ws');
        ws.onopen = function() {
            console.log('ws connected');
            somePackage.ws = ws;
        };
        ws.onerror = function() {
            console.log('ws error');
        };
        ws.onclose = function() {
            console.log('ws closed');
        };
        ws.onmessage = function(msgevent) {
            var msg = JSON.parse(msgevent.data);
            console.log('in :', msg);
            // message received, do something
        };
    };
    
    somePackage.send = function(msg) {
        if (!this.ws) {
            console.log('no connection');
            return;
        }
        console.log('out:', msg)
        this.ws.send(window.JSON.stringify(msg));
    };
    

    You call somePackage.connect to open the connection.
    After that, the message arrivals are handled in the onmessage handler and to send a message to the server you just call somePackage.send.

    Even if this is full of asynchronous calls, that doesn’t mean that your program won’t immediately react to the arrival of a message (as soon as your other functions have stopped working because you have only one thread).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i am trying to make function with while loop in php but cant
I'm trying to write a function to read csv contents into a datatable. I'm
I'm trying to make my program read/write in a database. I found this sample
I'm trying to write a wrapper function for read() system call , using asm
I'm trying to make a RLE (Run-Length Encoder) Programme just for characters. I read
I've been trying to pair NIO with multithreaded read handling in order to make
I'm trying to make a simple msn client mostly for fun but also for
I'm trying to user node-proxy to auto-magically read/write databases and I can't figure a
I am trying to write a C++ function that matches whether a string is
I'm trying to write a better bookmark manager in Chrome extensions. The problem is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.