简单的 Express 3.x socket.io 聊天
[英] simple Express 3.x socket.io chat
本文介绍了简单的 Express 3.x socket.io 聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里有一些代码来自一个非常流行的网络教程.为了使用 Express 3.x,进行了一些更改.
这里是app.js的代码:
var express = require('express'), http = require('http');var app = express();var server = http.createServer(app);var io = require('socket.io').listen(server);服务器.听(8000);//路由app.get('/', function (req, res) {res.sendfile(__dirname + '/index.html');});//当前连接到聊天的用户名var 用户名 = {};io.sockets.on('connection', function (socket) {//当客户端发出sendchat"时,它会监听并执行socket.on('sendchat', 函数(数据){//我们告诉客户端使用 2 个参数执行updatechat"io.sockets.emit('updatechat', socket.username, data);});//当客户端发出adduser"时,它会监听并执行socket.on('adduser', 函数(用户名){//我们将用户名存储在此客户端的套接字会话中socket.username = 用户名;//将客户端的用户名添加到全局列表中用户名[用户名] = 用户名;//回显给他们已经连接的客户端socket.emit('updatechat', 'SERVER', '你已经连接');//全局(所有客户端)回显一个人已连接socket.broadcast.emit('updatechat', 'SERVER', username + '已连接');//更新聊天中的用户列表,客户端io.sockets.emit('updateusers', 用户名);});//当用户断开连接时.. 执行此操作socket.on('断开', function(){//从全局用户名列表中删除用户名删除用户名[socket.username];//更新聊天中的用户列表,客户端io.sockets.emit('updateusers', 用户名);//全局回显这个客户端已经离开socket.broadcast.emit('updatechat', 'SERVER', socket.username + '已断开连接');});});
上述代码与网络教程中的代码相同,只是使用 Riwels 的回答 .
这里是 index.html 的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><脚本>var socket = io.connect('http://localhost:8080');//在连接到服务器时,使用匿名回调请求用户名socket.on('connect', function(){//调用服务端函数'adduser'并发送一个参数(提示的值)socket.emit('adduser', prompt("你叫什么名字?"));});//侦听器,每当服务器发出updatechat"时,就会更新聊天正文socket.on('updatechat', 函数(用户名,数据){$('#conversation').append('<b>'+用户名+':</b>'+数据+'<br>');});//侦听器,每当服务器发出updateusers"时,就会更新用户名列表socket.on('updateusers', function(data) {$('#users').empty();$.each(data, function(key, value) {$('#users').append('' + key + '
');});});//在页面加载时$(函数(){//当客户端点击 SEND 时$('#datasend').click( function() {var message = $('#data').val();$('#data').val('');//告诉服务器执行 'sendchat' 并发送一个参数socket.emit('sendchat', message);});//当客户端按下键盘上的 ENTER 键时$('#data').keypress(function(e) {if(e.which == 13) {$(this).blur();$('#datasend').focus().click();}});});<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"><b>用户</b><div id="用户"></div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;"><div id="对话"></div><input id="data" style="width:200px;"/><input type="button" id="datasend" value="send"/>
Socket.io 启动良好,但该应用程序尚未显示任何消息.你能让这个工作吗?
解决方案
index.html 中存在错误.我忘了像在 app.js 中那样更改端口.现在它完美地工作.
index.html 的工作代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><脚本>var socket = io.connect('http://localhost:8000');//在连接到服务器时,使用匿名回调请求用户名socket.on('connect', function(){//调用服务端函数'adduser'并发送一个参数(提示的值)socket.emit('adduser', prompt("你叫什么名字?"));});//侦听器,每当服务器发出updatechat"时,就会更新聊天正文socket.on('updatechat', 函数(用户名,数据){$('#conversation').append('<b>'+用户名+':</b>'+数据+'<br>');});//侦听器,每当服务器发出updateusers"时,就会更新用户名列表socket.on('updateusers', function(data) {$('#users').empty();$.each(data, function(key, value) {$('#users').append('' + key + '
');});});//在页面加载时$(函数(){//当客户端点击 SEND 时$('#datasend').click( function() {var message = $('#data').val();$('#data').val('');//告诉服务器执行 'sendchat' 并发送一个参数socket.emit('sendchat', message);});//当客户端按下键盘上的 ENTER 键时$('#data').keypress(function(e) {if(e.which == 13) {$(this).blur();$('#datasend').focus().click();}});});<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"><b>用户</b><div id="用户"></div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;"><div id="对话"></div><input id="data" style="width:200px;"/><input type="button" id="datasend" value="send"/>
Here is some code from a very popular webtutorial. Some changes were made in order to use Express 3.x.
here is the code of app.js:
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(8000);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
io.sockets.on('connection', function (socket) {
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
The obove code is the same code as in the webtutorial only some changes for Express 3.x were made using the answer of Riwels .
here is the code of index.html:
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
socket.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
Socket.io starts well but the app doesn't show any messages yet.
Can you get this to work?
解决方案
there was a bug in index.html. I forgot to change the port as i did in app.js.
Now it works perfectly.
the working code of index.html:
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('http://localhost:8000');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
socket.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
这篇关于简单的 Express 3.x socket.io 聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!