45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
class Websocket2Clinet{
|
|
// Constructor
|
|
constructor(server_addr){
|
|
if("WebSocket" in window){
|
|
console.log("您的浏览器支持Websocket");
|
|
ws = new WebSocket(server_addr);
|
|
ws.onopen = function(){
|
|
console.log("connected!");
|
|
}
|
|
|
|
ws.onmessage = function(evt){
|
|
var rece = evt.data;
|
|
console.log("received: "+ rece);
|
|
}
|
|
|
|
ws.onclose = function(){
|
|
console.log("disconnected!");
|
|
}
|
|
}else{
|
|
alert("您的浏览器不支持Websocket");
|
|
}
|
|
}
|
|
|
|
Broadcast(content, roomid){
|
|
// var data = {'tag': 'roomcast', 'msg': context, 'opt': true, 'arg': {'roomid': aROOM}};
|
|
// ws.send(JSON.stringify(data));
|
|
ws.send(JSON.stringify({'tag': 'roomcast', 'msg': content, 'opt': true, 'arg': {'roomid': roomid}}));
|
|
}
|
|
|
|
joinTheRoom(roomid){
|
|
// var Datas = {'tag': 'setRoom', 'msg': aROOM, 'opt': true};
|
|
// ws.send(JSON.stringify(Datas));
|
|
// console.log("Join to room " + aROOM);
|
|
// 结果返回
|
|
|
|
ws.send(JSON.stringify({'tag': 'setRoom', 'msg': roomid, 'opt': true}));
|
|
console.log("Join to room: " + roomid);
|
|
}
|
|
|
|
send(content){
|
|
// var data = {'tag': 'public', 'msg': context, 'opt': false};
|
|
// ws.send(JSON.stringify(data));
|
|
ws.send(JSON.stringify({'tag': 'public', 'msg': content, 'opt': false}));
|
|
}
|
|
} |