0

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

这是服务器代码:

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服务器中时,我再次发现了这种情况。我搞不清楚了。我认为我的网卡有问题。因为相同的代码在我朋友的机器上运行良好。

有没有人遇到过这种情况?或任何解决方案?

4

1 回答 1

1

您还可以尝试以下方法:

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

祝你好运 :)

于 2014-01-23T09:26:52.457 回答