使用Socket.io和Node.js的聊天应用程序
我正在使用Node.js和Socket.io开发聊天应用程序。 这是我的密码| socket.js使用Socket.io和Node.js的聊天应用程序,node.js,socket.io,putty,filezilla,Node.js,Socket.io,Putty,Filezilla,我正在使用Node.js和Socket.io开发聊天应用程序。 这是我的密码| socket.js var io = require('socket.io').listen(8001); var http = require('http'); var url = require('url'); var fs = require('fs'); // open the socket connection io.sockets.on('connection', function (socket) {
var io = require('socket.io').listen(8001);
var http = require('http');
var url = require('url');
var fs = require('fs');
// open the socket connection
io.sockets.on('connection', function (socket) {
// listen for the chat even. and will recieve
// data from the sender.
socket.on('chat', function (data) {
// default value of the name of the sender.
var sender = 'unregistered';
// get the name of the sender
socket.get('nickname', function (err, name) {
console.log('Chat Message By: ', name);
console.log('error ', err);
sender = name;
});
// broadcast data recieved from the sender
// to others who are connected, but not
// from the original sender.
socket.broadcast.emit('chat', {
msg : data,
msgr : sender
});
});
// listen for user registrations
// then set the socket nickname to
socket.on('register', function (name) {
// make a nickname paramater for this socket
// and then set its value to the name recieved
// from the register even above. and then run
// the function that follows inside it.
socket.set('nickname', name, function () {
// this kind of emit will send to all! :D
io.sockets.emit('chat', {
msg : "Hello " + name + '!',
msgr : "Mr.Server"
});
});
});
});
index.html
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script>
var name = '';
var socket = io.connect('http://localhost:8001');
// at document read (runs only ones).
$(document).ready(function(){
// on click of the button (jquery thing)
// the things inside this clause happen only when
// the button is clicked.
$("button").click(function(){
// just some simple logging
$("p#log").html('Sent message: ' + $("input#msg").val());
// send message on inputbox to server
socket.emit('chat', $("input#msg").val() );
$("p#data_recieved").append("<br />\r\n" + name + ': ' + $("input#msg").val());
// then we empty the text on the input box.
$("input#msg").val('');
});
$("#btnSubmit").click(function(){
alert("Disconnected");
//socket.clients[kickedUserSocketId].onDisconnect();
socket.close();
});
// ask for the name of the user, ask again if no name.
while (name == '') {
name = prompt("What's your name?","");
}
// send the name to the server, and the server's
// register wait will recieve this.
socket.emit('register', name );
});
// listen for chat event and recieve data
socket.on('chat', function (data) {
// print data (jquery thing)
$("p#data_recieved").append("<br />\r\n" + data.msgr + ': ' + data.msg);
// we log this event for fun :D
$("p#log").html('got message: ' + data.msg);
});
socket.emit('forceDisconnect');
</script>
</head>
<body>
<input type="text" id="msg"></input>
<button>Click me</button>
<p id="log"></p>
<p id="data_recieved"></p>
</body>
<input id = "btnSubmit" type="submit" value="Disconnect"/>
</html>
变量名=“”;
var socket=io.connect('http://localhost:8001');
//读取文档时(仅运行一个)。
$(文档).ready(函数(){
//单击按钮(jquery对象)
//本条款中的内容只有在
//点击按钮。
$(“按钮”)。单击(函数(){
//只是一些简单的日志记录
$(“p#log”).html('Sent message:'+$('input#msg”).val());
//将inputbox上的消息发送到服务器
emit('chat',$('input#msg”).val();
$(“接收到的数据”).append(
\r\n“+name+”:“++$(“输入消息”).val());
//然后我们清空输入框上的文本。
$(“输入消息”).val(“”);
});
$(“#btnsupmit”)。单击(函数(){
警报(“断开”);
//socket.clients[kickedUserSocketId].onDisconnect();
socket.close();
});
//询问用户的姓名,如果没有姓名,请再次询问。
while(name=''){
name=prompt(“你叫什么名字?”,”);
}
//将名称发送到服务器,以及服务器的
//注册等待将收到此消息。
socket.emit('寄存器',名称);
});
//监听聊天事件并接收数据
socket.on('chat',函数(数据){
//打印数据(jquery东西)
$(“收到的数据”).append(“
\r\n”+data.msgr+”:“+data.msg”);
//我们记录此事件是为了好玩:D
$(“p#log”).html('got message:'+data.msg);
});
socket.emit('forceDisconnect');
点击我
我正在命令提示符下运行first socket.js。之后,我在浏览器中运行了两次.html文件。现在2个用户可以通过浏览器聊天。但当我试图将我的.js文件和.html文件放在我使用FileZila和running.js文件创建的服务器上时,它正在运行,但当我试图在服务器端运行.html文件(在本例中为FileZila)时,通过给出它没有运行的服务器的IP地址和端口号。你能告诉我问题出在哪里吗?这似乎不是什么问题。 您的服务器应该可以通过类似“”的IP访问 如果浏览器中出现“无法连接”,请确保没有防火墙阻挡,并且可以从浏览器位置(也称为“您的电脑”)访问该端口(在示例中为80)。请注意,您的套接字服务在端口8001上,而您的web服务器可能在其他端口上运行?确保两个端口都已打开 如果您得到一个空白页面,请检查浏览器javascript错误消息
有可能是您忘记上载该目录“/socket.io/”中的文件“socket.io.js”。尝试直接下载该文件,看看您的服务器是否可用。您能澄清一下吗?“在服务器端运行.html文件”是什么意思?你怎么能看到“它没有运行”?您是否有可能忘记确保
socket.io.js
位于服务器端的路径中?run.html意味着客户端将通过提供IP地址.socket.io.js在浏览器上运行它,我必须在其中进行更改?