123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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}));
- }
- }
|