我是网络开发和网络技术的新手。为了学习,我正在尝试使用 websocket 创建多人游戏。游戏是“制作更大的单词”在这里我编写了一个代码,该代码将“随机”字母从基地发送到用户和拥有该字母 WIN 的更大单词的用户。
我有一个代码,但我在通过 socket.io API 与 base 连接时遇到了严重问题
给出创建单词的字母的代码:
<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 从 base 获取 a$ 但如何通过 socket.io 将这个 a$ 从服务器发送到客户端时,这非常容易
对于 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>
这是套接字服务器文件:
// 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 以从服务器到客户端获取 a$ ......如何让服务器将 aa$ 从基础发送到客户端???
(对不起我的英语,我是网络技术的初学者。很抱歉一个小问题)