按照官方文档,链接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>
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){
//关闭事件
}