How to handle WebSocket messages in an appmod using Yaws?

 ̄綄美尐妖づ 提交于 2019-12-10 10:47:00

问题


I have created a simple appmod that sends back the same message as it receives. But I get an error message at the command prompt and the WebSocket connection is closed.

If I send a message with 3 chars I get this error message:

=ERROR REPORT==== 8-Feb-2012::05:09:14 ===
Error in process <0.59.0> with exit value: {undef,[{mywebsocket,handle_message,[
{text,<<3 bytes>>}],[]},{lists,map,2,[{file,"lists.erl"},{line,1173}]},{yaws_web
sockets,loop,4,[{file,"yaws_websockets.erl"},{line,151}]}]}

What am I doing wrong? The handle uses text, and it doesn't work if I use binary either.

Here is my HTML+JavaScript client:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script>
window.onload = function() {
    document.getElementById('sendbutton').addEventListener('click', sendMessage, false);
    document.getElementById('connectbutton').addEventListener('click', connect, false);
    document.getElementById('disconnectbutton').addEventListener('click', disconnect, false);
}

function writeStatus(message) {
    var html = document.createElement("div");
    html.setAttribute("class", "message");
    html.innerHTML = message;
    document.getElementById("status").appendChild(html);
}

function connect() {
    ws = new WebSocket("ws://localhost:8090/ws.yaws");

    ws.onopen = function(evt) {
        writeStatus("connected");
    }

    ws.onclose = function(evt) {
        writeStatus("disconnected");
    }

    ws.onmessage = function(evt) {
        writeStatus("response: " + evt.data);
    }

    ws.onerror = function(evt) {
        writeStatus("error: " + evt.data);
    }
}

function disconnect() {
    ws.close();
}

function sendMessage() {
    var msg = document.getElementById('messagefield').value
    ws.send(msg);
}
</script>
</head>
<body>
<h1>Chat</h1>
<button id="connectbutton">Connect</button>
<button id="disconnectbutton">Disconnect</button><br/>
<input type="text" id="messagefield"/><button id="sendbutton">Send</button>
<div id="status"></div>
</body>
</html>

My ws.yaws that is called on connection from the client looks like this:

<erl>
out(A) -> {websocket, mywebsocket, []}.
</erl>

And my callback appmod mywebsocket.erl looks like this:

-module(mywebsocket).
-export([handle_message/1]).

handle_message({text, Message}) ->
    {reply, {text, Message}}.

In yaws.conf I have configured the server like this:

<server localhost>
    port = 8090
    listen = 0.0.0.0
    docroot = "C:\Users\Jonas/yawswww"
    appmods = <ws, mywebsocket>
</server>

I use Yaws 1.92 and Chrome 16.


回答1:


Probably, your appmod is not in PATH, or its module is not loaded in the yaws web server VM instance or shell. Try to call this method in the yaws shell once your web server starts:

1> mywebsocket:handle_message({text,"Some Data"}).
If it runs very well in the yaws shell, then it means that its in PATH. The error is undef meaning that the function call is failing because the module in which the function is contained, is not loaded.

Now, in the yaws.conf file, edit it to include the ebin folder where the compiled file of your appmod exists. Actually make sure to add all PATHS to executable code that your appmod depends on. it should appear like this:

# This the path to a directory where additional
# beam code can be placed. The daemon will add this
# directory to its search path

ebin_dir = "F:/programming work/erlcharts-1.0/ebin"
ebin_dir = "C:/SomeFolder/another_library-1.0/ebin"
ebin_dir = "D:/Any/Folder/myappmods"

# This is a directory where application specific .hrl
# files can be placed. application specifig .yaws code can
# then include these .hrl files

include_dir = "C:\Program Files (x86)\Yaws-1.90/examples/include"
include_dir = "D:/Any/Folder/myappmods/include"

Now, enter the PATHs to all your code in the yaws conf file. Do not worry about the forward slashes or back slashes, yaws always gets its way around Paths. For UNIX/LINUX it remains the same e.g. ebin_dir = "/usr/local/lib/erlang/lib/myapp-1.0/ebin".

However, note that your modules will not be loaded yet until yaws web server fully initialises



来源:https://stackoverflow.com/questions/9187809/how-to-handle-websocket-messages-in-an-appmod-using-yaws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!