Javascript 使用socket.io进行简单的多人游戏开发
socket.io多人游戏-从字母中生成更大的单词Javascript 使用socket.io进行简单的多人游戏开发,javascript,jquery,socket.io,Javascript,Jquery,Socket.io,socket.io多人游戏-从字母中生成更大的单词 var app = require('express').createServer() var io = require('socket.io').listen(app); app.listen(8080); // routing app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); // usernames which are
var app = require('express').createServer()
var io = require('socket.io').listen(app);
app.listen(8080);
// 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');
});
});
如何使用我的游戏:
var app = require('express').createServer()
var io = require('socket.io').listen(app);
app.listen(8080);
// 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');
});
});
和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>
var socket=io.connect('http://localhost:8080');
//在连接到服务器时,通过匿名回调请求用户名
socket.on('connect',function(){
//调用服务器端函数“adduser”并发送一个参数(prompt的值)
emit('adduser',prompt(“你叫什么名字?”);
});
//侦听器,无论何时服务器发出“updatechat”,都会更新聊天主体
socket.on('updatechat',函数(用户名、数据){
$(“#对话”).append(“+username+”:“+data+”
”);
});
//侦听器,每当服务器发出“updateusers”时,就会更新用户名列表
socket.on('updateusers',函数(数据){
$('#users').empty();
$。每个(数据、函数(键、值){
$(“#用户”).append(“”+key+“”);
});
});
//加载页面
$(函数(){
//当客户端单击SEND时
$('#datasend')。单击(函数(){
var message=$('#data').val();
$('数据').val('');
//告诉服务器执行“sendchat”并发送一个参数
发出('sendchat',消息);
});
//当客户端在键盘上按ENTER键时
$(“#数据”)。按键(功能(e){
如果(e.which==13){
$(this.blur();
$('#datasend')。焦点()。单击();
}
});
});
使用者
现在我需要使用socket.io允许两人游戏中的字母进行实时聊天,我已经有了socket.io聊天,但是如何将socket.io实现到我的游戏代码中,我在上面的JSFIDLE中介绍了这一点
请帮助理解socket.io游戏开发
我认为这对于理解socket.io创建游戏来说一定是一个很好的教程。
我想这里的很多人都会对这个问题感兴趣……你的问题很难理解,所以除了一般性的建议之外,我不太确定你想要什么 我快速搜索了一下,找到了这个
可能会有帮助?why-1 why…………好的,谢谢,但我认为这对许多人来说一定很有趣,因为他们想通过示例了解socket.io的工作原理。这不是一个真正的问题,建议您阅读常见问题解答。正如你们所问的,这个游戏和多用户客户端服务器无关。游戏内容相同,机制相同,聊天服务器是一个经典的例子…如何创建一个多层版本的这个;jsfiddle.net/9rtFa/12/witj实时连接和用户可能不是解决这个问题的好地方,我将删除himI jst。我不想允许多人游戏用于我的简单游戏jsfiddle.net/9rtFa/12/你不能只“允许”多人游戏。它必须被编程来处理多人互动。