[LayaAir 1.0]websocket连接出错
按照官方文档,链接socket 老是链接不上,然而自己写的能链接。
server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(8888);
console.log("服务器启动");
io.on('connection', function (ws) {
console.log('客户端连接成功!');
ws.on('foo', function(data){
console.log(data);
});
});
自己写的html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
<script type="text/javascript" src="js/socket.io.js"></script>
<script>
$(function(){
var socket = io.connect('10.10.1.103:8888');
socket.on('connect', function(){
socket.emit('foo', 'enenen');
});
});
</script>
</html>
laya的
Laya.init(800,600,Laya.WebGL);
// var socket = io.connect('10.10.1.103:8081');
this.byte = new Laya.Byte();
this.byte.endian = Laya.Byte.LITTLE_ENDIAN;
this.socket = new Laya.Socket();
this.socket.endian = Laya.Byte.LITTLE_ENDIAN;
//建立连接
this.socket.connectByUrl("ws://10.10.1.103:8888");
// this.socket.connect("10.10.1.103",8888);
this.socket.on(Laya.Event.OPEN, this, openHandler); //建立连接
this.socket.on(Laya.Event.MESSAGE, this, receiveHandler); //接收到数据触发函数
this.socket.on(Laya.Event.CLOSE, this, closeHandler); //关闭事件
this.socket.on(Laya.Event.ERROR, this, errorHandler); //连接出错
function openHandler(event){
//正确建立连接
this.socket.send('foo','OOOO');
}
function receiveHandler(msg){
console.log(event);
//接收到数据触发函数
// socket.emit('login',{username:123})
}
function closeHandler(e){
//关闭事件
}
server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(8888);
console.log("服务器启动");
io.on('connection', function (ws) {
console.log('客户端连接成功!');
ws.on('foo', function(data){
console.log(data);
});
});
自己写的html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
<script type="text/javascript" src="js/socket.io.js"></script>
<script>
$(function(){
var socket = io.connect('10.10.1.103:8888');
socket.on('connect', function(){
socket.emit('foo', 'enenen');
});
});
</script>
</html>
laya的
Laya.init(800,600,Laya.WebGL);
// var socket = io.connect('10.10.1.103:8081');
this.byte = new Laya.Byte();
this.byte.endian = Laya.Byte.LITTLE_ENDIAN;
this.socket = new Laya.Socket();
this.socket.endian = Laya.Byte.LITTLE_ENDIAN;
//建立连接
this.socket.connectByUrl("ws://10.10.1.103:8888");
// this.socket.connect("10.10.1.103",8888);
this.socket.on(Laya.Event.OPEN, this, openHandler); //建立连接
this.socket.on(Laya.Event.MESSAGE, this, receiveHandler); //接收到数据触发函数
this.socket.on(Laya.Event.CLOSE, this, closeHandler); //关闭事件
this.socket.on(Laya.Event.ERROR, this, errorHandler); //连接出错
function openHandler(event){
//正确建立连接
this.socket.send('foo','OOOO');
}
function receiveHandler(msg){
console.log(event);
//接收到数据触发函数
// socket.emit('login',{username:123})
}
function closeHandler(e){
//关闭事件
}
没有找到相关结果
已邀请:
要回复问题请先登录
1 个回复
Aar0n
赞同来自: