我在方法调用中遇到语法错误,我不明白:
The method connectToServer(Endpoint, ClientEndpointConfig, URI) in the type WebSocketContainer is not applicable for the arguments (WebsocketClientEndpoint, ClientEndpointConfig, URI)
课程WebsocketClientEndpoint
扩展Endpoint
,并且应与签名connectToServer(Class<? extends Endpoint> endpointClass, ClientEndpointConfig cec, URI path)
匹配。但它并没有。我在eclipse中遇到语法错误。
我错过了什么?
以下是代码:
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.xml.bind.DatatypeConverter;
import org.glassfish.tyrus.client.ClientProperties;
import org.glassfish.tyrus.client.auth.AuthConfig;
import org.glassfish.tyrus.client.auth.Credentials;
import org.glassfish.tyrus.core.wsadl.model.Endpoint;
/**
*/
@ClientEndpoint
public class WebsocketClientEndpoint extends Endpoint
{
Session userSession = null;
private MessageHandler messageHandler;
public WebsocketClientEndpoint(URI endpointURI)
{
try
{
ClientEndpointConfig.Configurator configurator = new ClientEndpointConfig.Configurator()
{
public void beforeRequest(Map<String, List<String>> headers)
{
headers.put("Authorization",
Arrays.asList("Basic " + DatatypeConverter.printBase64Binary("user:password".getBytes())));
}
};
ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().configurator(configurator)
.build();
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.connectToServer(this, clientConfig, endpointURI); //Signature mismatch here
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
... websocket callbacks
}
答案 0 :(得分:0)
connectToServer
需要javax.websocket.Endpoint
,但您传递的对象扩展为org.glassfish.tyrus.core.wsadl.model.Endpoint
,这是完全不兼容的类,并且不会扩展javax.websocket.Endpoint
。