-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial.html
71 lines (56 loc) · 2.65 KB
/
tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<html>
<head>
<title>SockIt · Quick Tutorial</title>
<link href="tutorial.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class='section'>
<center>
<h1>SockIt · Quick Tutorial</h1>
<h3><a href="downloads.html">Download</a></h3>
<br/>
<br/>
<a href="index.html">Home</a> · <a href="tutorial.html">Quick Tutorial</a> · <a href="full_tutorial.html">Full Tutorial</a> · <a href="demos.html">Demos</a> · <a href="api_documentation.html">API</a> · <a href="html/index.html">Source Documentation</a> · <a href="developers.html">Developers</a>
<br><br><br>
</center>
The plugin allows us to perform asynchronous network I/O in Javascript. To begin, load the plugin by calling
<pre>
var sockit = loadSockitPlugin();
</pre>
This function is found in <a href="scripts/sockit.js">sockit.js</a>, and returns the <code>window.sockit</code> object. Then we can start a (TCP) server listening on port 8080 (using IPv4):
<pre>
var server = sockit.createTcpServer(8080);
</pre>
And start it listening on the given port:
<pre>
server.listen();
</pre>
Since all of the network calls are asynchronous, the server needs to register a callback function in order to respond to events on the server. The API allows registering
<pre>
server.addEventListener('ondata', function(event) {
var message = event.read();
console.log(message); // log the message
event.send("world"); // respond to the client's message
});
</pre>
Now whenever the server receives data, the anonymous function provided will be invoked, so that the message received will be logged, and the server will reply with <code>"world"</code>. Since the plugin is already loaded, to start a (TCP) client:
<pre>
var client = sockit.createTcpClient("localhost", 8080);
</pre>
Now, we can listen for responses on the client connection by registering a callback on the client:
<pre>
client.addEventListener('ondata', function(event) {
alert(event.read());
});
</pre>
Finally, we can send data from this client to trigger the data callback on our server:
<pre>
client.send("hello");
</pre>
<p>To see this example in action, click <a href="../demos/hello_world/hello_world.html">here</a>.</p><br><br><br><br>
</div>
<center>
<a href="index.html">Home</a> · <a href="tutorial.html">Quick Tutorial</a> · <a href="full_tutorial.html">Full Tutorial</a> · <a href="demos.html">Demos</a> · <a href="api_documentation.html">API</a> · <a href="html/index.html">Source Documentation</a> · <a href="developers.html">Developers</a>
</center>
</body>
</html>