有没有大神?求分析,为什么netty-socketio连不上,十万火急,在线等!!!
server 代码:
package main.java;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
public class Server {
private static List<SocketIOClient> clients = new ArrayList<SocketIOClient>();//用于保存所有客户端
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setHostname("localhost");//设置主机名
configuration.setPort(8080);//设置监听的端口号
SocketIOServer server = new SocketIOServer(configuration);//根据配置创建服务器对象
server.addConnectListener(new ConnectListener() {//添加客户端连接监听器
public void onConnect(SocketIOClient client) {
System.out.println("connected:SessionId=" + client.getSessionId());
clients.add(client);//保存客户端
}
});
server.start();
System.out.println("server started");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Random random = new Random();
for(SocketIOClient client : clients) {
client.sendEvent("aaa", new Point(random.nextInt(100), random.nextInt(100)));//每隔一秒推送一次
}
}
}, 1000, 1000);
Object object = new Object();
synchronized (object) {
object.wait();
}
}
}
页面代码:
<!DOCTYPE html>
<html>
<head>
<title>netty-socketio测试</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<script src="/js/socket.io.js"></script>
<script src="/js/jquery-1.7.2.min.js"></script>
<script>
$(function(){
var socket = io.connect('http://localhost:8080');
//监听名为pushpoint的事件,这与服务端推送的那个事件名称必须一致
socket.on("aaa", function(data){
$('#x').text(data.x);
$('#y').text(data.y);
});
});
</script>
</head>
<body>
<div id="display" style="height:50px;background-color:grey;">
x=<span id="x">0</span>, y=<span id="y">0</span>
</div>
</body>
</html>