首先: 我用这个教程Java EE WebSocket创建了一个WebSocket,因为我不能使用SockJS或Stompway。我必须使用Plain Websocket。
现在有一段时间后,这是我的主控制器。 WSServerEndpint.java
package com.doka.websocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.socket.server.standard.SpringConfigurator;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;*
@Controller
@ServerEndpoint(value = "/var", configurator = SpringConfigurator.class, encoders={MessageEncoder.class, MessageEncoderBinary.class},
decoders={MessageDecoder.class, MessageDecoderBinary.class})
public class WSServerEndpoint {
@Autowired
ServerEndpointDelegateImpl delegate;
@PostConstruct
public void init()
{
}
@OnOpen
public void open(Session session) {
delegate.onConnect(session);
System.out.println("Session created with id :" + session.getId());
}
@OnClose
public void close(Session session) {
delegate.onDisconnect(session);
}
@OnError
public void onError(Throwable error) {
error.printStackTrace();
}
@OnMessage
public void onMessage(Session session, MessageText message) {
delegate.onMessage(session, message);
System.out.println(message);
}
@OnMessage
public void onMessageBinary(Session session, MessageBinary message)
{
delegate.onMessage(session, message);
System.out.println(message);
}
public WSServerEndpoint(){
}
如果我只删除configurator = SpringConfigurator.class
,则会运行,但@Autowired ServerEndpointDelegateImpl delegate
将始终为空。
我还尝试在构造函数中创建对象,如下所示:
public WSServerEndpoint(){
delegate = new ServerEndpointDelegateImpl();
}
然后应用程序启动,OnOpen也会正常运行,但稍后在Code中我正在使用follwing函数:
public class AuthenticationChannelImpl extends DefaultAuthentication implements AuthenticationChannel
{
@Autowired
private MongoDBApp mongoDBApp;
[...]
for(ConnectionProfile connectionProfile : mongoDBApp.getConnectionRepository().findAll()){
[...]
}
然后我和以前一样有同样的问题。 mongoDBApp将为null。
所以我的猜测是我在WSServerEndpoint.java中做错了什么,或者我使用@Autowired错误。
这是我的Endpointconfig.java:
package com.doka.websocket;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class EndpointConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
serverEndpointExporter.setAnnotatedEndpointClasses(WSServerEndpoint.class);
return serverEndpointExporter;
}
我也找到并尝试过的事情: (对不起格式错误,但是我并没有发布超过2个链接,但我认为这是相关的。)