如何使用Yaws在appmod中处理WebSocket消息?

时间:2012-02-08 04:22:20

标签: erlang websocket yaws

我创建了一个简单的 appmod ,它会在收到时发回相同的消息。但是我在命令提示符处收到错误消息,并且WebSocket连接已关闭。

如果我发送带有 3个字符的消息,我会收到以下错误消息:

=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}]}]}

我做错了什么?句柄使用text,如果我使用binary,它也不起作用。

这是我的HTML + JavaScript客户端:

<!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>

来自客户端的连接上调用的ws.yaws如下所示:

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

我的回调 appmod mywebsocket.erl如下所示:

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

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

yaws.conf我已按照以下方式配置服务器:

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

我使用 Yaws 1.92 Chrome 16

1 个答案:

答案 0 :(得分:2)

可能,您的appmod不在PATH中,或者其模块未在yaws Web服务器VM实例或shell中加载。尝试在Web服务器启动后在yaws shell中调用此方法:

1> mywebsocket:handle_message({text,"Some Data"}).
如果它在yaws shell中运行得很好,那么它意味着它在PATH中。错误为undef,意味着函数调用失败,因为未包含函数所在的模块。


现在,在yaws.conf文件中,编辑它以包含appmod的编译文件所在的ebin文件夹。实际上确保将所有PATHS添加到appmod所依赖的可执行代码中。它应该是这样的:

# 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"

现在,在yaws conf文件中输入所有代码的PATH。不要担心前倾斜线或反斜线,雅司总是绕过路径。对于UNIX / LINUX,它保持不变,例如ebin_dir = "/usr/local/lib/erlang/lib/myapp-1.0/ebin"

但请注意,在yaws web server完全初始化之前,您的模块将不会被加载