node服务器给客户端发消息,无法发送消息从客户端到服务器使用node.js和socket.io...

我尝试node.js和socket.io在几天内在Web上构建应用程序。我不明白为什么我不能发送消息从客户端到服务器使用socket.io和服务器node.js.我可以从服务器接收消息和事件到客户端。但不能从客户端的事件服务器接收。我想我的问题与我的库有关。无法发送消息从客户端到服务器使用node.js和socket.io

模块的node.js我安装

表达

IO

socket.io

socket.io客户端

为什么有插座。 io-client和socket.io?我甚至尝试这个例子:code.google.com/p/nodejs-win/wiki/SimpleChatSystem 但仍然无法正常工作。服务器无法接收来自客户端的任何按摩和事件。

代码server.js

// Require HTTP module (to start server) and Socket.IO

var http = require('http'),

io = require('socket.io');

// Start the server at port 8080

var server = http.createServer(function(req, res){

// Send HTML headers and message

res.writeHead(200,{ 'Content-Type': 'text/html' });

res.end('

Hello Socket Lover!

');

});

server.listen(8080);

// Create a Socket.IO instance, passing it our server

var socket = io.listen(server);

// Add a connect listener

socket.on('connection', function(client){

console.log("server is start on port 8080");

// Create periodical which ends a message to the client every 5 seconds

var interval = setInterval(function() {

client.send('This is a message from the server! ' + new Date().getTime());

},5000);

// Success! Now listen to messages to be received

client.on('message',function(event){

console.log('Received message from client! ',event);

});

client.on('disconnect',function(){

clearInterval(interval);

console.log('Server has disconnected');

});

});

代码客户端的index.html

* { margin:0; padding:0; font-size:11px; font-family:arial; color:#444; }

body { padding:20px; }

#message-list { list-style-type:none; width:300px; height:300px; overflow:auto; border:1px solid #999; padding:20px; }

#message-list li { border-bottom:1px solid #ccc; padding-bottom:2px; margin-bottom:5px; }

code { font-family:courier; background:#eee; padding:2px 4px; }

// Create SocketIO instance

var socket = new io.connect('localhost',{

port: 8080

});

//socket.connect();

// Add a connect listener

socket.on('connect',function() {

log('Client has connected to the server!');

});

// Add connecting listener

socket.on('connecting',function() {

log('Client is connecting to the server!');

});

// Add a connect listener

socket.on('message',function(data) {

log('Received a message from the server: ' + data);

});

// Add a disconnect listener

socket.on('disconnect',function() {

log('The client has disconnected!');

});

// Sends a message to the server via sockets

function sendMessageToServer(message) {

socket.emit("message",message);

socket.send(message);

log('Sending "' + message + '" to the server!');

}

// Outputs to console and list

function log(message) {

var li = document.createElement('li');

li.innerHTML = message;

document.getElementById('message-list').appendChild(li);

}

setInterval(function(){ sendMessageToServer(new Date())},5000);

Messages will appear below (and in the console).

  • Type socket.disconnect() to disconnect
  • Type socket.connect() to reconnect
  • Type sendMessageToServer('Your Message') to send a message to the server

+0

你检查出sockjs为您的应用。它简单而强大。 –

+0

谢谢...我会试试看。但问题仍然存在,如果我不能让它运行socket.io –

+0

我认为'套接字'尚未初始化'sendMessageToServer'里面。请在'sendMessageToServer'' console.log(typeof套接字)'里面检查一下。或尝试移动此行''setInterval(function(){sendMessageToServer(new Date())},5000);''inside' socket.on('connect',function(){' –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值