[]Socket能连接到服务器,客户端发请求也能收到服务端的数据,但是服务端主动推送数据,客户端就是收不到数据?
问题:Socket能连接到服务器,客户端发请求也能收到服务端的数据,但是服务端主动推送数据,客户端就是收不到数据?
前端主要是用字符串通信;服务端是以“\n”为接受标识的;
急人啊,我看了socket源码也没有发现什么需要设置的啊,关键是连接也没有断开,但就是接收不能主动接收服务端推送过来的数据,求解决,谢谢了
是否需要设置什么吗?
前端主要是用字符串通信;服务端是以“\n”为接受标识的;
急人啊,我看了socket源码也没有发现什么需要设置的啊,关键是连接也没有断开,但就是接收不能主动接收服务端推送过来的数据,求解决,谢谢了
是否需要设置什么吗?
要回复问题请先登录
8 个回复
sword2015
赞同来自:
package Scripts.Core
{
import laya.net.Socket;
import laya.utils.Byte;
import laya.events.Event;
import Scripts.Core.MsgManger;
import Scripts.Core.IHandler;
import Scripts.Core.TipsManager;
import laya.utils.Browser;
/**
* ...
* @author
*/
public class Socketmanger{
private static var _I:Socketmanger;
public static function get Instance():Socketmanger
{
if(_I==null)_I=new Socketmanger();
return _I;
}
private static var socket:Socket=null;
private var list:Array=;
public function Connect(url:String):void
{
socket=new Socket();
socket.on(Event.OPEN, this, onSocketOpen);
socket.on(Event.CLOSE, this, onSocketClose);
socket.on(Event.MESSAGE, this, onMessageReveived);
socket.on(Event.ERROR, this, onConnectError);
socket.connectByUrl(url);
}
public function Add(handler:IHandler):void
{
if(list.indexOf(handler) < 0){
list.push(handler);
}
}
public function Remove(handler:IHandler):void
{
var index:int=list.indexOf(handler);
if(index>=0){
list.splice(index,1);
}
}
public function GetHandler(key:String):IHandler
{
for(var i:int=0;i<list.length;i+=1)
{
var handler:IHandler=list.GetHandler(key);
if(handler!=null){
return handler;
}
}
return null;
}
public function Send(data:String):void
{
socket.send(data);
}
private function onSocketOpen(e:*=null):void
{
trace("Service connection successful!");
MsgManger.SendMsg("SockeConnetSucc");
}
private function onSocketClose(e:*=null):void
{
trace("Socket closed");
}
private function onMessageReveived(message:*=null):void
{
trace("============message=============");
if (message is String)
{
var dataObj:Object=JSON.parse(message);
trace("Message from server:"+dataObj+"-----------"+dataObj.status);
if(dataObj.messageType){
trace("Reveive message type"+dataObj.messageType);
}
if(dataObj!=null){
if(dataObj.status == "S" || (dataObj.body && dataObj.body.status == "S")){
var key:String=dataObj.messageType;
var handler:IHandler=GetHandler(key);
if(handler!=null){
handler.OnReveived(dataObj);
}
}else
{
var str:String=dataObj.errorMsg;
if(str.indexOf("-100")>=0){
str="This account is already logged in!";
}
TipsManager.Instance.ShowTips(str,"Confirm",OnCallBack);
}
}
}
else if (message is ArrayBuffer)
{
trace(new Byte(message).readUTFBytes());
}
socket.input.clear();
}
private function OnCallBack():void
{
MsgManger.SendMsg("LoginOut");
}
private function onConnectError(e:Event=null):void
{
trace("error");
}
}
}
写了个测试例子,可以收到服务器的推送的数据 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test WebSocket</title>
<script type="text/javascript">
//显示信息
var log = function(s) {
if (document.readyState !== "complete") {
log.buffer.push(s);
} else {
document.getElementById("output").textContent += (s + "\n");
document.getElementById("outputdiv").scrollTop = document.getElementById("outputdiv").scrollHeight;
}
}
log.buffer = [];
//显示连接状态
function setConnected(status) {
document.getElementById("socketstatus").innerHTML = status;
}
var ws = null;
//连接
function connect() {
if (ws != null) {
log("现已连接");
return ;
}
url = "ws://192.168.2.103:10000/websocket";
if ('WebSocket' in window) {
ws = new WebSocket(url);
} else if ('MozWebSocket' in window) {
ws = new MozWebSocket(url);
} else {
alert("您的浏览器不支持WebSocket。");
return ;
}
ws.onopen = function() {
log("open");
setConnected("已连接");
//设置发信息送类型为:ArrayBuffer
ws.binaryType = "string";
}
ws.onmessage = function(e) {
log(e.data.toString());
}
ws.onclose = function(e) {
log("closed");
}
ws.onerror = function(e) {
log("error");
}
}
//断开连接
function disconnect() {
if (ws != null) {
ws.close();
ws = null;
setConnected("已断开");
}
}
window.onload = function() {
connect();
log(log.buffer.join("\n"));
document.getElementById("sendButton").onclick = function() {
if (ws != null) {
//发送SubmitBetInfo请求
var submitBetInfo_obj = new Object();
var userBetList=[];
var bet1_obj = new Object();
bet1_obj.betType="1";
bet1_obj.amount=20;
userBetList.push(bet1_obj);
var bet2_obj = new Object();
bet2_obj.betType="4";
bet2_obj.amount=50;
userBetList.push(bet2_obj);
submitBetInfo_obj.userId=1;
submitBetInfo_obj.userBetList=userBetList;
submitBetInfo_obj.realTotalBetAmount=230;
submitBetInfo_obj.messageType="SubmitBetInfo";
alert(JSON.stringify(submitBetInfo_obj));
ws.send(JSON.stringify(submitBetInfo_obj));
}
}
document.getElementById("sendButton1").onclick = function() {
if (ws != null) {
//发送查询开盘剩余时间请求
var queryGameLeftTime_obj = new Object();
queryGameLeftTime_obj.messageType="QueryGameLeftTime";
alert(JSON.stringify(queryGameLeftTime_obj));
ws.send(JSON.stringify(queryGameLeftTime_obj));
}
}
}
</script>
</head>
<body onunload="disconnect();">
<div>连接状态:<span id="socketstatus"></span></div>
<div>
<button id="sendButton">发送下注请求</button>
</div>
<div>
<button id="sendButton1">发送查询开盘剩余时间请求</button>
</div>
<div>
<button id="connect" onclick="connect();">连接</button>
<button id="disconnect" onclick="disconnect();">断开</button>
</div>
<div style="height:300px; overflow:auto;" id="outputdiv">
<pre id="output"></pre>
</div>
</body>
</html>
cuixueying
赞同来自:
sword2015
赞同来自:
sword2015
赞同来自:
cuixueying
赞同来自:
sword2015
赞同来自:
sword2015
赞同来自:
代码如下:
var SocketJS={
ws:null,
OnOpenMsg:null,
OnMessage:null,
connect:function(url){
if ('WebSocket' in window) {
ws = new WebSocket(url);
} else if ('MozWebSocket' in window) {
ws = new MozWebSocket(url);
} else {
alert("您的浏览器不支持WebSocket。");
return ;
}
ws.onopen = function() {
console.log("---open----");
//设置发信息送类型为:ArrayBuffer
ws.binaryType = "string";
if(SocketJS.OnOpenMsg!=null)SocketJS.OnOpenMsg();
}
ws.onmessage = function(e) {
console.log(e.data.toString());
if(SocketJS.OnMessage!=null)SocketJS.OnMessage(e.data);
}
ws.onclose = function(e) {
console.log("closed");
}
ws.onerror = function(e) {
console.log("error");
}
},
send:function(data){
ws.send(data);
},
disconnect:function(){
if (ws != null) {
ws.close();
ws = null;
console.log("disconnect");
}
}
}
在index文件用直接嵌入
使用
sword2015
赞同来自: