Note that there are some explanatory texts on larger screens.

plurals
  1. POWeb game developing with socketIO
    primarykey
    data
    text
    <p>I'm new to web developing and web tehnologies. For learning I'm trying to cretate a multiplayer game with websocket. The game is "Make the bigger word" Here I write a code that get "random" letters from base to users and user who have a bigger word with that letter WIN.</p> <p>I have a code but I have a seriuos problem with connecting with base via socket.io API</p> <p>Code that give a letter for creating word:</p> <pre><code>&lt;div id="s"&gt;STOP&lt;/div&gt; &lt;div id="L1"&gt;&lt;/div&gt; &lt;div id="L2"&gt;&lt;/div&gt; &lt;div id="L3"&gt;&lt;/div&gt; &lt;div id="L4"&gt;&lt;/div&gt; &lt;div id="L5"&gt;&lt;/div&gt; $a="AGHBV"; v=setInterval(function(){for(i=0;i&lt;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&lt;$a.length;j++){$("#L"+(j+1)).html($a[j]);}},250);}); </code></pre> <p>When user click on STOP div then they get the $a letters... so that is so easy when you get a$ from base via php but how to send this a$ from server to client via socket.io</p> <p>for socket.io I have a some code but I doesnt know how need to exactly work.</p> <p>Here is socket client file:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; * { 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; } &lt;/style&gt; &lt;script src="http://cdn.socket.io/stable/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; // Create SocketIO instance var socket = new io.Socket('localhost',{ port: 8080 }); socket.connect(); // Add a connect listener socket.on('connect',function() { log('&lt;span style="color:green;"&gt;Client has connected to the server!&lt;/span&gt;'); }); // 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('&lt;span style="color:red;"&gt;The client has disconnected!&lt;/span&gt;'); }); // Sends a message to the server via sockets function sendMessageToServer(message) { socket.send(message); log('&lt;span style="color:#888"&gt;Sending "' + message + '" to the server!&lt;/span&gt;'); } // 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); }; }; */ &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Messages will appear below (and in the console).&lt;/p&gt;&lt;br /&gt; &lt;ul id="message-list"&gt;&lt;/ul&gt; &lt;ul style="margin:20px 0 0 20px;"&gt; &lt;li&gt;Type &lt;code&gt;socket.disconnect()&lt;/code&gt; to disconnect&lt;/li&gt; &lt;li&gt;Type &lt;code&gt;socket.connect()&lt;/code&gt; to reconnect&lt;/li&gt; &lt;li&gt;Type &lt;code&gt;sendMessageToServer('Your Message')&lt;/code&gt; to send a message to the server&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and here is socket server file:</p> <pre><code>// 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('&lt;h1&gt;Hello Socket Lover!&lt;/h1&gt;'); }); 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'); }); }); </code></pre> <p>What I need to do to get what I need. How to implement socketIO in my script to get a$ from server to client ... how to do that server send a a$ from base to client???</p> <p>(sorry for my english, i'm beginer to web tech. so sorry about a trivial question)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload