这几天 需要对misultin做一个简单的测试
就想到了rpc关键代码 rpc:multicall([node()|nodes()], misultin_websocket_sessions_example, rpc, [Data]),
顺便看了一眼rpc源代码,原来也是调用了otp中的gen_server中的方法
% callback on received websockets data
% ets:new(s,[named_table,public]).
handle_websocket(Ws) ->
io:format("~p Ws=~p~n", [?LINE, Ws]),
receive
{browser, "token:"++User} ->
Ws:send(["received '", User, "'"]),
ets:insert(s, {User, Ws}),
handle_websocket(Ws);
{browser, Data} ->
Ws:send(["received '", Data, "'"]),
%rpc(Data),
rpc:multicall([node()|nodes()], misultin_websocket_sessions_example, rpc, [Data]),
handle_websocket(Ws);
_Ignore ->
handle_websocket(Ws)
after 60000 ->
handle_websocket(Ws)
end.
rpc(Data) ->
ListWs = ets:tab2list(s),
[W:send(["From ':", Data, "'"]) || {_N, W}<-ListWs].
test js
<html>
<body>
<p id="socketStatus"></p>
<input type="text" id="inputMessage" value="hello websocket">
<input type="text" id="to" value="Bill">
<button id="sendButton">send</button>
<button id="sendButtonjson">sendjson</button>
</body>
{% block link %}
{% endblock %}
</html>
<script type="text/javascript">
//alert(window.WebSocket);
socket = new WebSocket("ws://192.168.18.119:8001/test", "some_service_name");
socket.onopen = function(){
//alert("connected");
//socket.send("accepting....");
updateSocketStatus("Connected to WebSocket ");
};
socket.onmessage = function(e){
//alert("message");
//alert(e.data);
updateSocketStatus(e.data);
//updateSocketStatus("update to WebSocket "+e);
//updateSocketStatus("update to WebSocket "+dataReturned(e.data));
};
socket.onclose = function(e){
// alert("close");
//alert(typeof(e));
updateSocketStatus("close "+e.data);
};
function updateSocketStatus(message){
msg = document.getElementById("socketStatus").innerHTML +"</br>"+ message;
document.getElementById("socketStatus").innerHTML = msg;
}
/**
*/
window.onload = function(){
document.getElementById("sendButton").onclick = function() {
var message = document.getElementById("inputMessage").value;
socket.send(message);
}
document.getElementById("sendButtonjson").onclick = function() {
var to = document.getElementById("to").value;
//var txt = '{"message" : [{ "to":"Bill" , "text":"Gates" }]}';
var txt = 'direct_messages/new/{ "to":"'+ to + '" , "text":"Gates" }';
socket.send(txt);
}
};
</script>
~
~