参考 https://tyrus.java.net/documentation/1.13/index/ 这里,用java实现了一个websocket,在本地可以运行,但是部署到LeanEngine后不能正常访问,代码和相关访问方法如下:
public class MyAppConfig implements ServerApplicationConfig {
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> set) {
return null;
}
@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> set) {
Set<Class<?>> s = new HashSet<>();
s.add(EventSocket.class);
return s;
}
}
@ClientEndpoint
@ServerEndpoint(value="/echo/")
public class EventSocket
{
Session session;
@OnOpen
public void onWebSocketConnect(Session sess)
{
System.out.println("Socket Connected: " + sess);
session = sess;
}
@OnMessage
public void onWebSocketText(String message) throws IOException {
System.out.println("Received TEXT message: " + message);
session.getBasicRemote().sendText(message);
}
@OnClose
public void onWebSocketClose(CloseReason reason)
{
System.out.println("Socket Closed: " + reason);
}
@OnError
public void onWebSocketError(Throwable cause)
{
cause.printStackTrace(System.err);
}
}
使用微信小程序提供的接口访问:
wx.connectSocket({
url: 'ws://leancloud.cn/echo/',
header: {
'Content-Type': 'application/json',
'X-LC-Id': 'xxx',
'X-LC-Key': 'xxx',
'X-LC-Prod': 1,
},
method:'POST',
success: function () {
console.log("ws_open success")
},
fail: function () {
console.log("ws_open fail")
}
});
返回错误: