2012-02-08 5 views
2

私は単純なを受信したときと同じメッセージを返すように作成しました。しかし、コマンドプロンプトでエラーメッセージが表示され、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> 

そして、私のコールバックappmodmywebsocket.erlルックスこのように:

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

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

私はこのようにサーバーを設定している:

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

私はイチゴ腫1.92クローム16を使用しています。

答えて

2

あなたのappmodがPATHにないか、またはそのモジュールがyaws WebサーバーのVMインスタンスまたはシェルにロードされていない可能性があります。あなたのWebサーバーが起動したら、yawsシェルでこのメソッドを呼び出そうとしてください。

 
1> mywebsocket:handle_message({text,"Some Data"}). 
yawsシェルでうまく動作するならば、それはPATHにあることを意味します。エラーは undefで、関数が含まれているモジュールがロードされていないために関数呼び出しが失敗しています。


さて、yaws.confファイルで、あなたのappmodのコンパイル済みのファイルが存在するebinフォルダが含まれるように、それを編集します。実際には、あなたのappmodが依存する実行可能コードにすべてのPATHSを必ず追加してください。

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

ここで、すべてのコードのPATHをyaws confファイルに入力します。スラッシュやバックスラッシュを心配しないでください。ヨーは常にパスの周りを歩き回ります。 UNIX/LINUXの場合、同じままです。 ebin_dir = "/usr/local/lib/erlang/lib/myapp-1.0/ebin"

しかし、イチゴ腫のWebサーバが完全に

+1

感謝を初期化するまで、あなたのモジュールがまだロードされないことに注意してください、これは働いていました。あなたの他の投稿の内容をこの回答に追加しました。新しい回答を投稿するのではなく、回答を編集する必要があります。 – Jonas

関連する問題