So i have been trying to make my canvas game work in real time multiplayer with long polling right now which connects to my mysql database, but I am now trying to switch to web sockets. I am a little confused on where the websockets in storing information and on how it is being organized when it is stored. Do the websockets connect to a mysql server? Does the information stored using websockets reset when the server resets? any help is appreciated. Thanks
So i have been trying to make my canvas game work in real time multiplayer with long polling right now which connects to my mysql database, but I am now trying to switch to web sockets. I am a little confused on where the websockets in storing information and on how it is being organized when it is stored. Do the websockets connect to a mysql server? Does the information stored using websockets reset when the server resets? any help is appreciated. Thanks
Share Improve this question asked Oct 31, 2012 at 17:48 user1754830user1754830 811 gold badge1 silver badge7 bronze badges 5- 2 Websockets is just a way to open a two way muncication of sorts between the browser and server, it does'nt store anything. – adeneo Commented Oct 31, 2012 at 17:50
- OK so does it work similarly to memcache in php? Does the web socket just create global persistent session variables in a sense? – user1754830 Commented Oct 31, 2012 at 17:54
- 1 It's just the persistent connection between client and server. Websockets is actually defined as an API by W3C. APIs don't store anything, they just provide an interface for information exchange. – William The Dev Commented Oct 31, 2012 at 17:56
- 1 What Websockets does is kinda like what Ajax does...you can't pare it to Memcache. – William The Dev Commented Oct 31, 2012 at 18:01
- I guess what I don't understand is how the information exchange works in php. Does it just create variables stored on the sever that all pages can municate with? Similar to how me cache works in php and application variables in asp. – user1754830 Commented Oct 31, 2012 at 18:06
2 Answers
Reset to default 4DISCLAIMER: This answer is directed to the OPs ments. The scripts presented are, by no means, secure. It's preferable to use a third party FW like Ratchet
I think you're misunderstanding how PHP works. Let me give you an example and build some explanation from that...
You have a webserver(example.) with 2 files: a.php and b.php.
a.php
<?php
$varA = "I'm var A";
echo $varA;
b.php
<?php
echo $varA;
to run script a.php you direct the browser to http://example./a.php. Output is:
I'm var A
However going to http://example./b.php will print a notice saying
Notice: Undefined variable: varA in /path/to/webroot/b.php on line 2
Why is that?
It's because both script are totally independent. They dont' even know of each other existence.
Now let's change b.php a bit:
b.php
<?php
include 'a.php';
echo $varA;
output:
I'm var AI'm var A
This basically tells b.php to include a.php, thus "sharing" variables, objects, class and function definitions.
POST and GET
Another way to pass data between scripts is using POST or GET.
c.php
<?php
if (isset($_GET['c']) {
$varC = $_GET['c'];
} else {
$varC = 'NONE';
}
echo $varD;
Going to http://example./c.php will output
NONE
Going to http://example./c.php?c=something will output
something
Passing a variable from d.php to c.php. You can use a GET request.
d.php
<?php
$varD = urlencode("i'm from d");
echo "<a href=\"http://example./c.php?c=$varD\">pass value</a>";
or
header('Location: http://example./c.php?c='.urlencode("i'm from d"));
Going to d.php and clicking pass value will output
i'm from d
Instead of using GET you can do a POST request. (we will cover that later)
$_SESSION
What about between "accesses"?
Each time you access a php file, the script is run from the beginning to the end.
Here's another file (e.php)
<?php
if (!isset($i)) {
$i = 0;
}
++$i;
This script tells you that if var $i is not defined, $i=0 and then increments it by one.
Accessing http://example./e.php will ALWAYS output 1. No data is stored between accesses.
Unless... you use the $_SESSION variable (or store the data in a persistent media).
ii.txt
0
e.php
<?php
session_start();
if (!isset($_SESSION['i'])) {
$_SESSION['i'] = 0;
}
++$_SESSION['i'];
$ii = file_get_contents('ii.txt');
++$ii;
file_put_contents('ii.txt', $ii);
echo "session counter: " . $_SESSION['i'];
echo '<br/>';
echo "file counter: " . $ii;
Each time you access e.php, both counters will increase.
BUT... $_SESSION variable is not a persistent media. When the session is destroyed (or expires), session counter will reset. However, the file counter will always increase, so it's a persistent media. You can, of course, use a database to store the variable. The principle is the same.
Passing variables between different servers:
The principle is the same when passing variables between file in the same server. However, you can't (normally) include or require a php file located in another server. Moreover, it is best when sending information from two locations to secure the data. Here's an example using a socket connection.
a.php (located at client.)
<?php
//Our Data
$dataArray = array('foo' => 'some data', "bar" => 42);
// Data convertion into URL parameters -> foo=some%20data&bar=42
$data = http_build_query($dataArray);
//extract the parts of the url
$url = parse_url("http://server./b.php");
$host = $url['host']; //server.
$path = $url['path']; //b.php
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
//HEADERS
fputs($fp, "POST $path HTTP/1.1\r\n"); //POST method
fputs($fp, "Host: $host\r\n"); //The host
fputs($fp, "Referer: myApp\r\n"); //who's the referer
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); //Content type: a form post that is url encoded
fputs($fp, "Content-length: ". strlen($data) ."\r\n"); //data length (size in chars)
fputs($fp, "Connection: close\r\n\r\n");
//DATA
fputs($fp, $data);
$result = '';
// Request result
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
} else {
// Something went bad
echo "ERROR: $errstr ($errno)";
}
// Socket close
fclose($fp);
//SUCCESS
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
echo "HEADER: $header<br><br>";
echo "CONTENT:<br>$content";
b.php (located at server.)
<?php
header('Content-type: text/plain');
if (isset($_POST)) {
file_put_contents('data.txt', $_POST, FILE_APPEND);
file_put_contents('data.txt', PHP_EOL, FILE_APPEND);
print file_get_contents('data.txt');
} else {
echo "NOT OK";
}
when going to http://client./a.php a POST request is sent to b.php. If it is successful, b.php stores the data in a file called data.txt and returns the contents of that file.
Hope this helps understanding sockets and PHP.
Your post is very confused.
with long polling right now which connects to my mysql database
AFAIK there's no direct bridge from a browser to a mysql database - since you've tagged this as PHP, the I assume there's a script in between.
where the websockets in storing information
Websockets don't store information they are a conduit for transferring data.
Do the websockets connect to a mysql server?
No - you still need something in between. Even if yopu could implement the MySQL protocol in javascript you wouldn't be able to access the database this way - websockets tunnel a datagram based protocol via a stream (TCP) based connection. As to what the in between thing should be, yes PHP can be part of the solution.
There are lots of websocket servers / adapters out there, many of which are either implemented in PHP o support a PHP backend - try googling for them and read through the example code.
Does the information stored using websockets reset when the server resets?
See above - websockets don't store information.