FranXho
FranXho

Reputation: 765

websocket server not receiving message

First I built a websocket server using node js and ws module. Then using chrome and firefox, I connect to that server and the connection is successfully established. However, the message I send from browsers does not arrive at the server. I have some code on server to console.log out if message is received. Nothing appears, however when I refresh the browser, the messages I previously sent arrive. The messages did not arrive when sent them but only once I refresh the page. I don't know why. This seems to work in from some other computers but not mine.

Here is the server code:

var WebSocketServer = require('ws').Server
  , http = require('http')
  , express = require('express')
  , app = express();

app.use(express.static(__dirname + '/views'));
var rmi = require('./RMIClient.js');
console.log(rmi);

var server = http.createServer(app);
server.listen(8080);
var wss = new WebSocketServer({server: server});
// from here is the logic codes
var clients = [];
var clientId = 0;
wss.on('connection', function(ws) {
    console.log("connection established for client "+ (clients.length+1)); 
    clients.push(ws);
    console.log("index is " + clients.indexOf(ws));
    clientId += 1;

    ws.send("Hello Client: " + clientId);
//
//  ws.send("Welcome from AMTT Chatting Server");
    ws.on('message',function(data){
        console.log('message receieved : '+data);
        for(var i = 0;i<clients.length;i++){
            clients[i].send(data);
        }       
    });
    ws.on('a',function(){
        console.log("a event fire from client");
    });
    ws.on('close', function() {
        var index = clients.indexOf(ws);
        console.log('stopping client interval '+index);
        if (index > -1) {
            clients.splice(index, 1);
        }
    });
});

Here is the client code:

<html>
<script>
    //var ws = new WebSocket('ws://localhost:8080/');
    var messagearea,inputarea,sendButton;
    var connection = new WebSocket(/*'wss://echo.websocket.org');*/'ws://192.168.8.195:8080/');
    // When the connection is open, send some data to the server
    console.log(connection.readyState);
    connection.onopen = function () {
        console.log(connection.readyState);
        inputarea.disabled = false;
        sendButton.disabled = false;
    };

    // Log errors
    connection.onerror = function (error) {
      console.log('sorry connection fail:' + JSON.stringify(error));
    };

    // Log messages from the server
    connection.onmessage = function (e) {
        messagearea.value = messagearea.value + '\n' + e.data;
        console.log('Server: ' + e.data);

    };

    function sendMessage(){
        if(inputarea.value !='')
        connection.send(inputarea.value);
        inputarea.value = '';
    }

</script>
<body>
    <textarea rows="15" cols="100" id="messagearea" disabled>   
    </textarea>
    <br/>
    <textarea rows="2" cols="90" id="inputarea" required autofocus> 
    </textarea>
    <input type = 'button' value = 'send' id = 'sendbutton' onclick = "sendMessage()"/>

</body>
<script>
    messagearea = document.getElementById('messagearea');
    messagearea.value = '';
    inputarea = document.getElementById('inputarea');
    inputarea.value = '';
    inputarea.disabled = true;
    sendButton = document.getElementById('sendbutton');
    sendButton.disabled = true;
</script>

</html>

And again I found that kind of situation when I develop that code in java and deployed in wildfly server. I am lost. I think there is something concerned with my network card. Because that same code work perfectly in my friend's machine.

Does anybody experience this situation ? or any solution?

Upvotes: 0

Views: 3583

Answers (1)

Dako Dakov
Dako Dakov

Reputation: 21

You can also try the following:

connection.addEventListener("message", function (e) {
    processSocketMessage(e);
});

good luck :)

Upvotes: 1

Related Questions