我是網絡開發和網絡技術的新手。爲了學習,我試圖用websocket創建一個多人遊戲。遊戲是「製作更大的單詞」在這裏,我寫了一個代碼,它可以從基地獲得「隨機」字母的用戶和使用WIN的較大單詞的用戶。用socketIO開發的網絡遊戲
我有一個代碼,但我有一個問題seriuos通過socket.io API與基座連接
代碼,讓一個字母創建字:
<div id="s">STOP</div>
<div id="L1"></div>
<div id="L2"></div>
<div id="L3"></div>
<div id="L4"></div>
<div id="L5"></div>
$a="AGHBV";
v=setInterval(function(){for(i=0;i<6;i++){$("#L"+i).html(String.fromCharCode(Math.floor(Math.random()*26+65)))};},500);$("#s").click(function(){clearInterval(v);setTimeout(function(){for(j=0;j<$a.length;j++){$("#L"+(j+1)).html($a[j]);}},250);});
當STOP DIV用戶點擊即可他們得到了$ a的信...所以這很容易,當你通過php從基地獲得$時,但如何通過socket.io發送這個$從服務器到客戶端
for socket.io我有一些代碼,但我不知道如何正確工作。
下面是套接字客戶端文件:
<!DOCTYPE html>
<html>
<head>
<style>
* { 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; }
</style>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Create SocketIO instance
var socket = new io.Socket('localhost',{
port: 8080
});
socket.connect();
// Add a connect listener
socket.on('connect',function() {
log('<span style="color:green;">Client has connected to the server!</span>');
});
// 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('<span style="color:red;">The client has disconnected!</span>');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
log('<span style="color:#888">Sending "' + message + '" to the server!</span>');
}
// Outputs to console and list
function log(message) {
var li = document.createElement('li');
li.innerHTML = message;
document.getElementById('message-list').appendChild(li);
}
/*
// Create a socket instance
socket = new WebSocket('ws://localhost:8080');
// Open the socket
socket.onopen = function(event) {
console.log('Socket opened on client side',event);
// Listen for messages
socket.onmessage = function(event) {
console.log('Client received a message',event);
};
// Listen for socket closes
socket.onclose = function(event) {
console.log('Client notified socket has closed',event);
};
};
*/
</script>
</head>
<body>
<p>Messages will appear below (and in the console).</p><br />
<ul id="message-list"></ul>
<ul style="margin:20px 0 0 20px;">
<li>Type <code>socket.disconnect()</code> to disconnect</li>
<li>Type <code>socket.connect()</code> to reconnect</li>
<li>Type <code>sendMessageToServer('Your Message')</code> to send a message to the server</li>
</ul>
</body>
</html>
,這裏是socket服務器文件:
// 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('<h1>Hello Socket Lover!</h1>');
});
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){
// 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');
});
});
我需要做的就是什麼,我需要什麼。如何在我的腳本中實現socketIO以獲得$從服務器到客戶端...如何做到這一點的服務器從基地發送$到客戶端?
(對不起我的英語,我很beginer到網絡技術。大約一個簡單的問題,很抱歉)
也在這裏工作的版本:http://jsfiddle.net/ Hx28c/3/ – 2012-04-24 20:26:57
沒人知道? ... – 2012-04-24 20:31:08
你基本上說過,這裏是我所有的代碼,它有什麼不對?通常你會有一個非常具體的問題,只有相關的代碼片段。這是一個非常模糊的問題,需要一段代碼才能查看。 – dqhendricks 2012-04-24 20:37:05