websocket服务器没有收到消息

时间:2013-11-01 02:24:17

标签: javascript node.js websocket

首先,我使用节点js和ws模块构建了一个websocket服务器。然后使用chrome和firefox,我连接到该服务器并成功建立连接。但是,我从浏览器发送的消息没有到达服务器。如果收到消息,我在服务器上有一些代码来调控.log。什么都没有出现,但是当我刷新浏览器时,我之前发送的消息到达了。发送邮件时邮件没有到达,但只有一次我刷新页面。我不知道为什么。这似乎适用于其他一些计算机而不是我的计算机。

这是服务器代码:

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);
        }
    });
});

以下是客户端代码:

<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>

当我在java中开发代码并部署在wildfly服务器中时,我再次发现了这种情况。我搞不清楚了。我认为我的网卡有一些问题。因为相同的代码在我朋友的机器上完美运行。

有人遇到过这种情况吗?还是任何解决方案?

1 个答案:

答案 0 :(得分:1)

您还可以尝试以下操作:

connection.addEventListener("message", function (e) {
    processSocketMessage(e);
});
祝你好运:)