如何使用Yaws处理appmod中的WebSocket消息? [英] How to handle WebSocket messages in an appmod using Yaws?

查看:120
本文介绍了如何使用Yaws处理appmod中的WebSocket消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

如果我发送带有 3个字符的消息,我得到这个错误消息:

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.

这是我的HTML + JavaScript客户端:

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>

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

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

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

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

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

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

yaws.conf 我已经配置这样的服务器:

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>

我使用的是Yaws 1.92 和 Chrome 16

I use Yaws 1.92 and Chrome 16.

推荐答案

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

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"}).


如果它在yaws shell中运行得很好,那就意味着它在PATH中。错误是 undef 意味着函数调用失败,因为包含该函数的模块未加载。

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.

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

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"

现在,输入路径你的所有代码在yaws conf文件。不要担心正斜杠或反斜杠,雅士总是绕路径走。对于UNIX / LINUX,它保持不变。 ebin_dir =/usr/local/lib/erlang/lib/myapp-1.0/ebin

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

这篇关于如何使用Yaws处理appmod中的WebSocket消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆