Test WebSocket

$(function() {varurl= "ws://localhost:8889/webChat/websocket";varws= "";varmessage={"id":"","msg":"","form":"","to":""};functionconnection() {

ws= newWebSocket(url);

console.log("connection.......");

ws.onmessage= function(e){varjson=eval('(' +e.data.toString()+ ')');

showMessage(json.from+":"+json.msg);

}

ws.onclose= function() {

showMessage("close");

}

ws.onerror= function(e){

showMessage("error");

}

ws.onopen= function() {

showMessage("链接成功")

message.id=$(".identity").val();

message.msg= "newUser";

console.log(JSON.stringify(message));

ws.send(JSON.stringify(message));

message.msg= "";

}

}

$(".start-conn-btn").click(function() {

connection();

});

$(".send-btn").click(function() {//send message

message.to=$(".to-user").val();

message.msg=$(".msg-context").val();

$(".msg-context").val("");

ws.send(JSON.stringify(message));

showMessage("我:" +message.msg );

message.msg= "";

});functionshowMessage(msg) {

$(".show-message").append( msg+ "
");

}

});

ID:

链接

toUser:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐