私は単純なのを受信したときと同じメッセージを返すように作成しました。しかし、コマンドプロンプトでエラーメッセージが表示され、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を使用しています。
感謝を初期化するまで、あなたのモジュールがまだロードされないことに注意してください、これは働いていました。あなたの他の投稿の内容をこの回答に追加しました。新しい回答を投稿するのではなく、回答を編集する必要があります。 – Jonas