GithubHelp home page GithubHelp logo

wse's Introduction

wse - Websockets for Erlang

WSE is a simple, standalone, websocket server which execute request in the browser instead of in the web server. There is a bit of boot strapping that may need help from a web server. But a simple web page and a browser is all that it takes to get running.

To get control of part of a web page (or all of it) use the following HTML snippet. Also make sure you have ej.js and wse.js in the same directory.

<html><head>
<title>wse demo page</title>
<script src='ej.js'></script>
<script src='wse.js'></script>
<script>
    window.onload = function() {
      if (Wse.open("ws://localhost:1234/websession"))
         Wse.start('wse_demo', 'run', ["myid"]);
    };
</script></head>
<body>
  <div id="myid"></div>
</body></html>

Next thing is to start erlang wse server (default to port 1234):

$ erl
> wse_server:start().

Then have the erlang module wse_demo in the path somewhere:

-module(wse_demo).
-export([run/2]).

run(Ws, Where) ->
    ElemNode = wse:createElement(Ws, "p"),
    TextNode = wse:createTextNode(Ws, "Hello world"),
    wse:appendChild(Ws, ElemNode, TextNode),
    wse:appendChild(Ws, wse:id(Where), ElemNode).

The browser will call wse_demo:run (via the websocket) with the web socket proxy process as a the first argument and the "myid" as the second argument. From thereon the web page can be manipulated at will.

register pages

One nice trick to interact with pages from command line is to register the page it self

<html><head>
<title>Page A</title>
<script src='ej.js'></script>
<script src='wse.js'></script>
<script>
  window.onload = function() {
  if (Wse.open("ws://localhost:1234/websession")) {
    Wse.register('page_a');
  };
</script>
</head><body>
<p id="x">Hello A World</p>
</body></html>

Now this page should be registered in erlang and can be tested. Start with changing the text in the paragraf on the page.

> {ok,X} = wse:getElementById(page_a, "x").
> {ok,Text} = wse:firstChild(page_a, X).
> wse:set(page_a, Text, "nodeValue", "Hej A").

Or we could send some ehtml (erlang style html) defining a button to it

> wse:send(page_a, "x", {button,[{id,"y"}],["Press Me"]}).

Or why not send a whole table to it

> wse:send(page_a, "x", {table,[],[{tr,[],[{td,[],["A"]},{td,[],["B"]}]},{tr,[],[{td,[],["C"]},{td,[],["D"]}]}]}).

To have events sent to us when pressing a button, using send, we can do

> {ok,E} = wse:create_event(page_a).
> wse:send(page_a, "x", {button,[{id,"y"},{onclick,"Wse.notify("++integer_to_list(E)++",'click');"}],["Press Me"]}).

Click on the button on page A a couple of times

> flush().
Shell got {notify,2,[],"click"}
Shell got {notify,2,[],"click"}
Shell got {notify,2,[],"click"}

wse's People

Contributors

malotte avatar tonyrog avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.