Added websocket examples for the server and client.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
note
|
||||
description: "ws_client application root class"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
SHARED_EXECUTION_ENVIRONMENT
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
ws_client: EXAMPLE_WS_CLIENT
|
||||
do
|
||||
-- create ws_client.make_with_port ("wss://echo.websocket.org", 443, Void)
|
||||
-- if ws_client.is_ssl_supported and ws_client.is_tunneled then
|
||||
-- ws_client.set_ssl_certificate_file ("ca.crt")
|
||||
-- ws_client.set_ssl_key_file ("ca.key")
|
||||
-- else
|
||||
-- -- Not supported!
|
||||
-- end
|
||||
|
||||
create ws_client.make_with_port ("ws://echo.websocket.org", 80, Void)
|
||||
-- create ws_client.make_with_port ("ws://127.0.0.1", 9090, Void)
|
||||
ws_client.launch
|
||||
ws_client.join_all
|
||||
|
||||
execution_environment.sleep (5_000_000)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,78 @@
|
||||
note
|
||||
description: "Summary description for {EXAMPLE_WS_CLIENT}."
|
||||
author: ""
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
EXAMPLE_WS_CLIENT
|
||||
|
||||
inherit
|
||||
WEB_SOCKET_CLIENT
|
||||
|
||||
create
|
||||
make, make_with_port
|
||||
|
||||
feature -- Initialization
|
||||
|
||||
make (a_uri: STRING; a_protocols: detachable LIST [STRING])
|
||||
do
|
||||
initialize (a_uri, a_protocols)
|
||||
create implementation.make (create {WEB_SOCKET_NULL_CLIENT}, a_uri)
|
||||
end
|
||||
|
||||
make_with_port (a_uri: STRING; a_port: INTEGER; a_protocols: detachable LIST [STRING])
|
||||
do
|
||||
initialize_with_port (a_uri, a_port, a_protocols)
|
||||
create implementation.make (create {WEB_SOCKET_NULL_CLIENT}, a_uri)
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
count: INTEGER
|
||||
|
||||
feature -- Event
|
||||
|
||||
on_open (a_message: STRING)
|
||||
do
|
||||
print (a_message)
|
||||
print ("%NProtocol:" + protocol)
|
||||
on_text_message (a_message)
|
||||
end
|
||||
|
||||
on_text_message (a_message: STRING)
|
||||
local
|
||||
l_message: STRING
|
||||
do
|
||||
if count <= 10 then
|
||||
print ("%NMessage is %"" + a_message + "%".%N")
|
||||
print ("%NCount:" + count.out)
|
||||
create l_message.make_empty
|
||||
l_message.append ("mesg#" + count.out)
|
||||
send (l_message)
|
||||
count := count + 1
|
||||
else -- Send close initiated by the client
|
||||
close (1001)
|
||||
end
|
||||
end
|
||||
|
||||
on_binary_message (a_message: STRING)
|
||||
do
|
||||
send_binary (a_message)
|
||||
end
|
||||
|
||||
on_close (a_code: INTEGER; a_reason: STRING)
|
||||
do
|
||||
ready_state.set_state ({WEB_SOCKET_READY_STATE}.closed)
|
||||
end
|
||||
|
||||
on_error (a_error: STRING)
|
||||
do
|
||||
end
|
||||
|
||||
connection: like socket
|
||||
do
|
||||
Result := socket
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-15-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-15-0 http://www.eiffel.com/developers/xml/configuration-1-15-0.xsd" name="ws_client" uuid="AF6EDC56-D7B4-4E1F-A62B-40EBED3D93DF">
|
||||
<target name="common">
|
||||
<file_rule>
|
||||
<exclude>/.git$</exclude>
|
||||
<exclude>/.svn$</exclude>
|
||||
<exclude>/CVS$</exclude>
|
||||
<exclude>/EIFGENs$</exclude>
|
||||
</file_rule>
|
||||
<option warning="true" is_obsolete_routine_type="true" void_safety="transitional">
|
||||
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
|
||||
</option>
|
||||
<setting name="console_application" value="true"/>
|
||||
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
|
||||
<library name="web_socket_client" location="..\..\web_socket_client-safe.ecf" readonly="false"/>
|
||||
<cluster name="ws_client" location=".\" recursive="true"/>
|
||||
</target>
|
||||
<target name="ws_client" extends="common">
|
||||
<root class="APPLICATION" feature="make"/>
|
||||
<setting name="concurrency" value="thread"/>
|
||||
</target>
|
||||
<target name="ws_client_ssl" extends="ws_client">
|
||||
<variable name="net_ssl_enabled" value="true"/>
|
||||
</target>
|
||||
</system>
|
||||
15
library/network/websocket/client/www/index.html
Normal file
15
library/network/websocket/client/www/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WebSocket prototype</title>
|
||||
<script src="js/jquery-1.10.1.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebSocket prototype</h1>
|
||||
<button id="run">Run</button>
|
||||
<p>Output :</p>
|
||||
<div id=console>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
9807
library/network/websocket/client/www/js/jquery-1.10.1.js
vendored
Normal file
9807
library/network/websocket/client/www/js/jquery-1.10.1.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
37
library/network/websocket/client/www/js/main.js
Normal file
37
library/network/websocket/client/www/js/main.js
Normal file
@@ -0,0 +1,37 @@
|
||||
$(function() {
|
||||
var wsEchoURI = "ws://localhost:9999/";
|
||||
var websocket;
|
||||
|
||||
function printConsole(text) {
|
||||
$("#console").append($(document.createElement("p")).html(text));
|
||||
}
|
||||
|
||||
function onWSOpen(evt) {
|
||||
printConsole("WebSocket open");
|
||||
if(websocket.readyState == websocket.OPEN) {
|
||||
printConsole("WebSocket ready");
|
||||
}
|
||||
}
|
||||
|
||||
function onWSClose(evt) {
|
||||
printConsole("WebSocket closed");
|
||||
printConsole(websocket.readyState);
|
||||
}
|
||||
|
||||
function onWSMessage(evt) {
|
||||
printConsole(evt.data);
|
||||
}
|
||||
|
||||
function onWSError(evt) {
|
||||
printConsole("An error occured in the WebSocket : " + evt.data);
|
||||
}
|
||||
|
||||
$("#run").click(function buttonRun() {
|
||||
printConsole("Connection");
|
||||
websocket = new WebSocket(wsEchoURI);
|
||||
websocket.onopen = function(evt) { onWSOpen(evt) };
|
||||
websocket.onclose = function(evt) { onWSClose(evt) };
|
||||
websocket.onmessage = function(evt) { onWSMessage(evt) };
|
||||
websocket.onerror = function(evt) { onWSError(evt) };
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user